Format date in the indexSuccess.php

a 夏天 提交于 2019-12-01 21:03:44

You can make in symfony in your indexSuccess.php and showSuccess.php instead of for example:

 <?php $value->getCreatedAt() ?>

next:

 <?php echo date('d.M.Y', strtotime($value->getCreatedAt())) ?>

You can use other formats.

The format of some data absolutely not belongs into controller context - so please use

use_helper("date");
echo format_date($myDate);

from symfony's date helper in your template (showSuccess.php, blaSuccess.php) or partial (form.php, list.php, test.php) !

You'll find more informations here http://www.symfony-project.org/gentle-introduction/1_4/en/13-I18n-and-L10n#chapter_13_sub_outputting_data_in_the_user_s_culture or in the source file.

I believe the date is returned in a string format Y-m-d H:i:s, as fits the MySQL datetime type. It would be nicest to convert this to a PHP DateTime instance using DateTime::createFromFormat, presuming you are using PHP > 5.3.

So, in your controller:

$this->creation_date = DateTime::createFromFormat('Y-m-d H:i:s', $item->created_at);

Then, in your view:

<?php echo $creation_date->format('d.F.Y') ?>

See also DateTime::format


If you are using PHP 5.2 or before, you can't use createFromFormat. You probably want to fall back on strtotime:

$this->creation_date = strtotime($this->created_at);

and in the view:

<?php echo date('d.F.Y', $creation_date) ?>

To create a datestring like your example (03.May.2011) use this way:

<?php use_helper("date"); ?>
<?php echo format_date($myDate,'dd. MMM. yyyy'); ?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!