【问题标题】:Show date with custom month name, php使用自定义月份名称显示日期,php
【发布时间】:2016-01-18 05:17:46
【问题描述】:

我是 php 新手,不知道如何操作字符串或日期。我有自定义月份名称,存储在数组中:

$months = ['1' => 'my Jan', '2'=>'my Feb', '3'=>'my Mar', '4'=>'my Apr', //etc.];

我想用这些月份名称显示日期,目前我显示:

date_format(new DateTime($route->start_date), 'd F Y')

这给了01 January 2016

我需要得到01 my Jan 2016

谢谢

【问题讨论】:

  • 来自哪里$route->start_date?来了,你能证明一下吗?
  • 它来自Active Record,在数据库中有date的类型。
  • 这样??? 01 我的 1 月,02 我的 2 月 .....
  • 还是这样的??? 1 月 18 日,2 月 18 日?/

标签: php yii2 yii2-advanced-app


【解决方案1】:

希望这会有所帮助。

<?php
$months = ['1' => 'my Jan', '2'=>'my Feb', '3'=>'my Mar', '4'=>'my Apr'];
$date = new DateTime($route->start_date);
echo '<br>'.$date->format('d').' '.$months[$date->format('n')].' '.$date->format('Y');

?>

【讨论】:

  • 你不应该把这样的逻辑放在视图中,因为它违反了 MVC 和 DRY。
  • 在我的情况下,它在控制器中,所以工作正常,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
  • 1970-01-01
  • 2014-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多