In my application,I retrieve a timestamp from the table which is of the format 2009-08-18 12:09:01. I need to change this to August 18th,2009 or 18 Aug,2009. How to achieve this in CakePHP?
Are there any built in methods?
In my application,I retrieve a timestamp from the table which is of the format 2009-08-18 12:09:01. I need to change this to August 18th,2009 or 18 Aug,2009. How to achieve this in CakePHP?
Are there any built in methods?
You can use the core TimeHelper
to format and test dates/times.
To use it, you need to add the TimeHelper
to your controller's $helpers
array:
var $helpers = array('Time');
Then from the view you can format your date/time variables:
// assuming $variable['Model']['field_name'] = '2009-09-09 12:00:00':
echo $time->format('F jS, Y', $variable['Model']['field_name']);
// => 'September 9th, 2009'
echo $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 date()
function to determine how to write the format string.
As of 1.3, if you reverse the order of the parameters (date first, format second) it will try to respect the date format of your locale. The above ordering will still work for backwards compatibility. More details at the bottom of this page in the migration guide.
Use the time helper as mentioned by deizel.
I would look through the file /cake/libs/view/helpers/time.php to see if there is a function there that provides the time format you are after.
The relative time functions are especially nice:
<?php echo $time->nice($dateValue); ?>
<?php echo $time->timeAgoInWords($dateValue); ?>
You can learn a great deal from reading the CakePHP source.
This should do:
<?php echo date('M js Y', strtotime($post['Post']['date'])); ?>
Here, Post is the name of the model and date is the field name.