当substr()返回字符串时,strtotime()函数不一致

时间:2019-06-14 14:58:34

标签: php strtotime substr

我正在使用substr()从长文本中获取日期字符串,该长文本是使用preg_match()函数找到的日期。 然后,我使用strtotime()将此字符串转换为纪元时间戳,因为我需要与已经采用该格式的另一个日期进行比较。

它的工作方式似乎不一致,经过一些测试,我似乎找不到该错误的来源。尽管substr()返回的日期字符串总是正确的,但是strtotime()函数并不总是有效。请注意,substr()是从一个较大的字符串中提取的,该字符串本身来自数据库。问题可能来自隐藏字符还是格式?

<?php
$DateString = substr($text, $matches[sizeof($matches) - 1][1] - 22, 17); //$DateString = the expected '2018-03-21 08:04' when echo
$DateString_hardcoded = '2018-03-21 08:04';

$DateEpoch = strtotime($DateString); //$DateEpoch is null
$DateEpoch_hardcoded = strtotime($DateString_hardcoded); //$DateEpoch_hardcoded return the expected 1521619440
?>

我不明白的是,有时字符串日期会使用完全相同的代码正确转换。例如,当

substr($text, $matches[sizeof($matches) - 1][1] - 22, 17) = '2010-08-23 16:10', $DateEpoch = 1282579800

符合预期。

1 个答案:

答案 0 :(得分:0)

结果是我只是不知道如何计数,而对于substr()的起始索引,我不得不执行-21,而不是-22。 我以一个不错的约会而告终,但一开始就有空白。我不知道为什么回声时没有显示。 解决了。​​