开发者

Format date in the indexSuccess.php

开发者 https://www.devze.com 2023-03-31 12:57 出处:网络
I want to format the created_at field date from the original for something like 03.May.2011 to be shown in the indexSuccess.php and in the showSuccess.php

I want to format the created_at field date from the original for something like 03.May.2011 to be shown in the indexSuccess.php and in the showSuccess.php Coul开发者_开发百科d you help me? thanks


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'); ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消