首页 > 解决方案 > 一年中的 PHP 错误日

问题描述

我正在尝试将一年中的某一天转换为日期。

所以没问题,我将 DateTime::createFromFormat 与 z 和 Y 一起使用。

DateTime::createFromFormat('z Y', '199 2020');

/*RESULT*/
object(DateTime)[3]
  public 'date' => string '2020-07-19 12:45:24.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Paris' (length=12)

但是当我测试结果时,我得到了错误的一年中的一天......

date('z Y', strtotime('2020-07-19'));

/*RESULT*/
'198 2020' (length=8)

所以我尝试这个

DateTime::createFromFormat('z Y', date('z Y', strtotime('2020-07-19')));

/*RESULT*/
object(DateTime)[3]
  public 'date' => string '2020-07-20 12:52:10.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Paris' (length=12)

如果我在此网站上查看 2020 年 199 年的某一天:https ://www.calendrier.best/numero-de-jour-2020.html

我得到 2020-07-17

我做错了什么??

标签: phpdatedatetime

解决方案


PHP 只检测一些标准,如您可以在此处看到的https://www.php.net/manual/de/datetime.formats.date.php

在您的第一个示例中,您定义了格式但strtotime()没有此类参数

如果您将这些值作为变量,您可以mktime()使用https://www.php.net/manual/de/function.mktime

但我建议总是在一月前给 PHP 一个月


推荐阅读