Magento – Change date format

dateformat

I use a plugin that shows me list of posts and I want to change format of date.
This is show me date like this:

24 january 2015 12:20:11
and this is the code of above output:

<?php echo $post->getCreatedTime(); ?>

But I want to show date like this:

24 january 2015

I use this code but this is show time in wrong month and date, for example instead of 24 january 2015 it shows me: Oct 12, 2015 !!!

<?php $_ct= new Zend_Date($post->getCreatedTime()); echo $_ct->toString('MMM dd, yyyy'); ?>

I don't have any idea why this is happen!!!

How can I fix it?

Best Answer

Don't forget about locale! As all other examples can cause issues for people from different time zones.

Mage::app()->getLocale()->date()

inside date() you can put any date and call method ->toString() with some param.

Pattern examples you can find in Varien_Date

const DATETIME_INTERNAL_FORMAT = 'yyyy-MM-dd HH:mm:ss';
const DATE_INTERNAL_FORMAT = 'yyyy-MM-dd';

const DATETIME_PHP_FORMAT       = 'Y-m-d H:i:s';
const DATE_PHP_FORMAT           = 'Y-m-d';

or use any you like.

For example for current time full call will look like this:

Mage::app()->getLocale()->date()->toString(Varien_Date::DATE_INTERNAL_FORMAT);