将此字符串转换为时间戳 PHP

2022-01-13 00:00:00 timestamp php date-conversion strtotime

我有这个字符串:13/10 15:00",我想将它转换为时间戳,但是当我这样做时:

I have this string: "13/10 15:00" and I would like to convert it to timestamp but when I do this:

      $timestamp = strtotime("13/10 15:00");

它返回一个空值.

推荐答案

在您的代码中 strtotime() 正在尝试将 13/10 转换为第 10 天第 13 个月,返回错误.

In your code strtotime() is attempting to convert 13/10 as the tenth day of the 13th month, which returns an error.

如果要解析自定义格式的日期字符串,最好使用 DateTime::createFromFormat() 代替:

If you want to parse a date string with a custom format, it's better to use DateTime::createFromFormat() instead:

$dtime = DateTime::createFromFormat("d/m G:i", "13/10 15:00");
$timestamp = $dtime->getTimestamp();

相关文章