3

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?

2

Use strtotime() function. See updated code below.

$_ct= new Zend_Date(strtotime($post->getCreatedTime()));
echo $_ct->toString('MMM dd, yyyy');    // will output Jan 24, 2015
  • When I use your code my page messed up and all data remove from page. – Elham Gdz Aug 10 '15 at 6:58
  • just wrap the parameter in Zend_Date function with strtotime() – Sukeshini Aug 10 '15 at 7:02
  • How can I do it? I am new in magento, please explain me more, thanks. – Elham Gdz Aug 10 '15 at 7:05
  • just change this line and check $_ct= new Zend_Date(strtotime($post->getCreatedTime())); – Sukeshini Aug 10 '15 at 7:07
  • When I just use <?php $_ct= new Zend_Date(strtotime($post->getCreatedTime())); ?> all data of my page will remove again!!! – Elham Gdz Aug 10 '15 at 7:12
2

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);
2

I have use today this format which is working fine.

$createdtime = new Zend_Date(strtotime($post->getCreatedTime()));
echo Mage::getModel('core/date')->date('M d, Y', $createdtime);
1

Just you need to set your local time zone. so it's get your server time and print as you wish.

date_default_timezone_set('Europe/Rome');

set your time zone. check if any problem tell me.

Happy Codding . :)

0

Use something like this:

$date = $post->getCreatedTime();
$dateToShow = date('j F Y',strtotime($date));
echo $dateToShow;

About the exact format just use: http://php.net/manual/en/function.date.php

  • Your code show me this date: 1 January 1970 instead of 24 january 2015!!! – Elham Gdz Aug 10 '15 at 7:04
  • I m using the same.Either you did something wrong with the copy paste or the $date is null – vbak Aug 10 '15 at 7:17
  • No, I copy your code properly and $date isn't null because I created post that the date of each post show properly in admin panel. – Elham Gdz Aug 10 '15 at 7:21
0

Please try below code

$anyDate = '24 january 2015 12:20:11';
echo $currentDate = Mage::getModel('core/date')->date('d F Y', $anyDate);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.