Format date in the indexSuccess.php

后端 未结 4 1630
礼貌的吻别
礼貌的吻别 2021-01-20 14:53

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 sh

相关标签:
4条回答
  • 2021-01-20 15:32

    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.

    0 讨论(0)
  • 2021-01-20 15:34

    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) ?>
    
    0 讨论(0)
  • 2021-01-20 15:35

    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.

    0 讨论(0)
  • 2021-01-20 15:44

    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 讨论(0)
提交回复
热议问题