How do I change the timestamp format in CakePHP?
You can use the core TimeHelper
to format and test date / time.
To use it you need to add array TimeHelper
to your controller $helpers
:
var $helpers = array('Time');
Then, from the view, you can format the date / time variables:
// assuming $variable['Model']['field_name'] = '2009-09-09 12:00:00':
echo $this->Time->format('F jS, Y', $variable['Model']['field_name']);
// => 'September 9th, 2009'
echo $this->Time->format('j M Y', $variable['Model']['field_name']);
// => '9 Sep 2009'
Since this method is ultimately a wrapper, use the table in the PHP documentation for the function date()
to determine how to write the format.
Since version 1.3, if you change the order of the parameters (date first and then in the second format), it will try to evaluate your language's date format. The above ordering will still work for backward compatibility. More details at the bottom of this page in the migration guide.
source to share
Use the time helper as deizel mentioned.
I went through the /cake/libs/view/helpers/time.php file to see if there is a function in there that provides the time format you are after.
The relative functions of time are especially nice:
<?php echo $time->nice($dateValue); ?>
<?php echo $time->timeAgoInWords($dateValue); ?>
You can learn a lot from reading the CakePHP source.
source to share