【问题标题】:Format date in the indexSuccess.php在 indexSuccess.php 中格式化日期
【发布时间】:2011-11-04 10:29:19
【问题描述】:

我想将 created_at 字段日期从原始格式格式化为 03.May.2011 之类的内容,以便在 indexSuccess.phpshowSuccess.php 中显示 你可以帮帮我吗? 谢谢

【问题讨论】:

    标签: date symfony1 format symfony-1.4


    【解决方案1】:

    你可以在你的 indexSuccess.php 和 showSuccess.php 中使用 symfony 来代替例如:

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

    下一个:

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

    您可以使用other 格式。

    【讨论】:

      【解决方案2】:

      某些数据的格式绝对不属于控制器上下文 - 所以请使用

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

      来自模板中的 symfony 日期助手(showSuccess.php、blaSuccess.php)或部分(form.php、list.php、test.php)!

      您可以在此处http://www.symfony-project.org/gentle-introduction/1_4/en/13-I18n-and-L10n#chapter_13_sub_outputting_data_in_the_user_s_culture 或源文件中找到更多信息。

      【讨论】:

      • 非常感谢。所以如果我有这个代码:&lt;?php echo $sgps_breves-&gt;getCreatedAt() ?&gt; 我应该写use_helper("date") echo format_date($sgps_breves-&gt;getCreatedAt())?
      【解决方案3】:

      我相信日期以字符串格式Y-m-d H:i:s 返回,适合 MySQL 日期时间类型。最好使用 DateTime::createFromFormat 将其转换为 PHP DateTime 实例,假设您使用的是 PHP > 5.3。

      所以,在你的控制器中:

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

      那么,在你看来:

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

      另见DateTime::format


      如果您使用的是 PHP 5.2 或更早版本,则不能使用createFromFormat。你可能想回到strtotime

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

      在视图中:

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

      【讨论】:

      • 我把你写的第一行代码放在哪里?我是 symfony 的新手……仍然不知道 symfony 创建的所有文件。顺便谢谢你:)
      • 控制器将是actions.class.php,在您模块的actions 文件夹中。
      • 我将第一行放在action.class.phpexecuteIndex 函数中,它得到了这个错误:Fatal error: Call to undefined method DateTime::createfromformat() in C:\wamp\www\sgps\apps\frontend\modules\breves\actions\actions.class.php on line 18
      • @Eva 好的,所以您必须使用 PHP 5.2。在这种情况下,请参阅我的编辑。
      【解决方案4】:

      要创建一个日期字符串,如您的示例 (03.May.2011),请使用以下方式:

      <?php use_helper("date"); ?>
      <?php echo format_date($myDate,'dd. MMM. yyyy'); ?>
      

      【讨论】:

        猜你喜欢
        • 2019-01-29
        • 2017-02-13
        • 2021-01-23
        • 2017-12-10
        • 2014-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多