【问题标题】:PHP - number of month from datePHP - 从日期开始的月份数
【发布时间】:2014-05-04 15:08:41
【问题描述】:

我正在尝试使用以下方法获取日期的月数:

$newDate = date("Y-m-d", strtotime('2014-05-04'));  
$Month = date('n', $newDate);
echo $Month;

它返回 1(这是一月...)这怎么可能?它应该返回 0。

因为那个帖子,我使用了日期格式:

PHP: date function to get month of the date

我希望有人可以帮助我。

提前致谢

希凡

【问题讨论】:

  • $newDatestring,而date() 的第二个参数应该是int(时间戳)
  • $Month = date('n', strtotime($newDate));
  • 我很好奇你为什么期待 0?因为你认为它应该是列表中的第一个数字? PHP 返回任何人都可以识别的月份数。 date('n') = 1, date('m') = 01

标签: php date format


【解决方案1】:

$Month = date('m', strtotime('2014-05-04'));

你可以得到这样的月份

【讨论】:

    【解决方案2】:

    对于日期时间操作,您应该在 PHP 中使用 DateTime

    $date = new DateTime('2014-05-04');
    echo $date->format('n');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-18
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多