gpt4 book ai didi

php - strtotime 和 date 输出不正确

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:43:44 24 4
gpt4 key购买 nike

我无法为数据库中保存的日期回显正确的 unix 时间戳。

$activeWorkRow['WorkDate'] = "9/24/2015 1:45:53 PM";
$locateDate = $activeWorkRow['WorkDate'];
$locateDate = str_replace('/', '-', $locateDate);
//$locateDate = date('m-d-Y', strtotime($locateDate));
//$locateDate = strtotime(date($locateDate));

echo $locateDate."<br>";

输出:

9-24-2015 1:45:53 PM

下一步:

$locateDate = $activeWorkRow['WorkDate'];
$locateDate = str_replace('/', '-', $locateDate);
$locateDate = date('m-d-Y', strtotime(locateDate));
//$locateDate = strtotime(date($locateDate));

输出:

12-31-1969

我正在尝试获取 unix 时间戳,以便与其他人进行比较。

最佳答案

您的问题是 str_replace('/', '-', $locateDate);。您的日期以 m/d/y 形式给出,通过替换您将其转换为 m-d-y

但是 strtotime- 视为欧洲 d-m-y 格式的指示符。在这种格式下,9/24/2015 不是有效日期,导致您观察到的输出(等于 date('m-d-Y', 0) ).

只需跳过str_replace:

$activeWorkRow['WorkDate'] = "9/24/2015 1:45:53 PM";
$locateDate = $activeWorkRow['WorkDate'];
echo strtotime($locateDate);

输出:

1443116753

比照。 the manual有关更多详细信息和注意事项:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.

请注意,在显示这些时间戳时,您可能还需要考虑时区(strtotime 使用默认值,除非另有说明),但只要您只比较由同一函数生成的时间戳,您应该没问题。

关于php - strtotime 和 date 输出不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39213507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com