My database stores two dates in the mysql DateTime format YYYY-MM-DD HH:MM:SS. When I get this data (with other strings etc), I want to convert it to another format, maybe DD.MM.YYYY HH:MM:SS and display it on my view in a table cell. My database dates are called date_begin and date_end.
Better, when I get this dates from database, convert it to DD.MM.YYYY format, separate the date and the time, store the time in a custom string ("HH1:MM1 - HH2:MM2") and bring both on my view.
How can I achieve this? I found some examples to convert on the view, not in the controller, but I think this is not good for MVC.
Not sure where you've gotten the impression that "formatting the date in the view is not good for MVC", because that's not a problem whatsoever.
If you're using Eloquent Models you can do it very easily:
1. Add the columns to the $dates property in your model class:
protected $dates = ['date_begin', 'date_end'];
This will ensure that the values get mutated to Carbon instances.
2. In your view files you can use the format method that Carbon offers like so:
<!-- To use only the date with the given format -->
{{ $item->date_begin->format('Y.m.d') }}
<!-- To use only the time with the given format -->
{{ $item->date_begin->format('H:i:s') }}
<!-- To use both date and time with the given format -->
{{ $item->date_begin->format('Y.m.d H:i:s') }}
There's no need to split the value in time and date, just show what you want from the DateTime value using whatever format you want.
If you're not using Eloquent models, then you can manually use Carbon to format your value like so:
{{ Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $item->date_begin)->format('Y.m.d') }}
I have used the PHP solution without Carbon:
Get the database date as you do (with the default format: YYYY-MM-DD HH:MM:SS) and when you print it on your view replace $item->date_begin
to:
date('d-m-Y H:i:s', strtotime($item->date_begin))
After that, when you save it on the database add on the DB update:
date('Y-m-d H:i:s', strtotime($request->date_begin))
protected $casts = [
'inicio' => 'datetime:Y-m-d\TH:i'
,'entrega' => 'datetime:Y-m-d\TH:i'
];
you could use it for the datetime-local field. greed you. like me if work for you
// attributes that should be cast to native types
protected $casts = [
'email_verified_at' => 'datetime'
];
// get formatted datetime string for email_verified_at
public function getEmailVerifiedAttribute()
{
if ($this->email_verified_at) {
$date = Carbon::createFromFormat('Y-m-d H:i:s', $this->email_verified_at, 'UTC');
return $date->setTimezone($this->timezone->name)->isoFormat('LLLL');
} else {
return null;
}
}
as Laravel document way.
I use jqGrid and my grid definition is like that:
...
colNames:['Type','Date','Message','User Name','Host'],
colModel:[{name:'type',index:'type', width:100},
{name:'date',index:'date', sorttype:'date', formatter:'date',
formatoptions: {newformat:'d-M-Y'}, width:100},
{name:'log',index:'log', width:200},
{name:'username',index:'username', width:50},
{name:'host',index:'host', width:50}],
...
When I debug my coming data one of the date value (it is Number) is as follows:
1322550786997
Grid shows it like that:
29-Nov-2011
Everything is OK till this point. However when I want to sort my date column it doesn't change anything.
Any ideas?
The problem is that decoding of the Unix (formatoptions: {srcformat: 'U', newformat: 'd-M-Y'}) date 1322550786997 get us 19-Dec-43879 and not 29-Nov-2011. You correct representation of the date will be the string "\/Date(1322550786997)\/" instead of the number 1322550786997.
See the demo:
UPDATED: You can also use the following custom formatter as a workaround
formatter: function (cellval, opts) {
var date = new Date(cellval);
opts = $.extend({}, $.jgrid.formatter.date, opts);
return $.fmatter.util.DateFormat("", date, 'd-M-Y', opts);
}
It creates Date and then use original date formatter to convert it to the format 'd-M-Y'. See here the demo.
I've implemented a JSON and an RSS feed into FullCalendar [only currently pulling events from JSON]. Using CodeIgniter I've created my JSON using the following code
<?php
$jsonevents = array();
foreach($events->result() as $entry){
$jsonevents[] = array(
'id' => $entry->eventID,
'title' => $entry->eventTitle,
'start' => $entry->startDate,
'end' => $entry->endDate,
'allDay' => false
);
}
echo json_encode($jsonevents);
?>
The following outputs this JSON
[{"id":"1234567891","title":"Test 3","start":"2011-05-07 00:00:00","end":"2011-05-13 00:00:00","allDay":false},
{"id":"24242","title":"sdfsdfsdfsdf","start":"2011-05-05 13:00:53","end":"2011-05-06 17:00:19","allDay":false},
{"id":"1234567890","title":"Test","start":"2011-05-05 13:00:53","end":"2011-05-06 17:00:19","allDay":false}]
The issue is when imported into FullCalendar it creates an event within an event, I believe it might have something to do with the square brackets, but I'm unsure of how to correct the output, anyone have any ideas?
Your json is right.
I've validated it with http://json.bloople.net/
You may have another problem when applying it to your calendar plugin.
I've just formatted your json string with JSON Formatter & Validator and it's valid.
The square brackets mean that the whole thing is an array. Please refer to json.org.
Json is right.. yay.
I also had this problem ;)
Make all day True and look below on the time.
2011-05-07 00:00:00
try notice the 'T'
2011-05-07T00:00:00
Here is a sample of my complex feed
[{"title":"Tee Times","start":"2011-05-03T00:00:00","end":"2011-05-03T00:00:00","allDay":true,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"Tee Times","start":"2011-05-04T00:00:00","end":"2011-05-04T00:00:00","allDay":true,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"Tee Times","start":"2011-05-05T00:00:00","end":"2011-05-05T00:00:00","allDay":true,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"Tee Times","start":"2011-05-06T00:00:00","end":"2011-05-06T00:00:00","allDay":true,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"","start":"2011-05-03T07:10:00","end":"2011-05-03T08:00:00","allDay":false,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"Test","start":"2011-05-04T07:00:00","end":"2011-05-04T07:50:00","allDay":false,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"Riley","start":"2011-05-04T08:00:00","end":"2011-05-04T08:00:00","allDay":false,"color":"rgb(21,144,51)","groupsize":"4","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"Test","start":"2011-05-04T08:10:00","end":"2011-05-04T17:50:00","allDay":false,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"pea","start":"2011-05-05T08:10:00","end":"2011-05-05T08:10:00","allDay":false,"color":"rgb(21,144,51)","groupsize":"1","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"peter","start":"2011-05-06T07:10:00","end":"2011-05-06T07:10:00","allDay":false,"color":"rgb(21,144,51)","groupsize":"1","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"March Medal - Mens Medal - CSS: 70 - PAR: 70 - Click for more info...","start":"2011-03-15T00:00:00","end":null,"allDay":null,"color":"rgb(10,87,164)","groupsize":null,"className":"data-previous clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":"http://www.howdidido.co.uk/ClubDetails.aspx?section=24&pagesection=compresultsdetails&compid=383","CompName":"March Medal","CompCourseName":"White Tee's","CompNumberDivs":1,"CompQualifierYN":"N","CompNumber":383}]