Php 无法使用strotime检查时间是否已过
我正在检查某个时间是否已经过去。解决方案1起作用,但使用strotime的解决方案2和3不起作用。如果strotime解决方案在日期不太遥远的情况下工作正常,那么您知道为什么strotime解决方案在该日期失败吗(例如,使用27.05.2035有效)Php 无法使用strotime检查时间是否已过,php,Php,我正在检查某个时间是否已经过去。解决方案1起作用,但使用strotime的解决方案2和3不起作用。如果strotime解决方案在日期不太遥远的情况下工作正常,那么您知道为什么strotime解决方案在该日期失败吗(例如,使用27.05.2035有效) 最大32位整数无法表示2038年1月19日之后的日期 解决办法是: 使用DateTime对象,这些对象不使用自1970年以来经过的秒数表示日期,而是为每个时间单位使用一个字段 使用64位版本的PHP,其中最大整数要高得多 有关更多详细信息,请参见。
最大32位整数无法表示2038年1月19日之后的日期 解决办法是:
DateTime
对象,这些对象不使用自1970年以来经过的秒数表示日期,而是为每个时间单位使用一个字段有关更多详细信息,请参见。您是否安装了32位PHP?如果是,则不能使用早于2038年的日期。此时,用于时间值的内部有符号32位整数将溢出并返回到1901。这似乎就是问题所在。万分感谢!那么现在,2038年之后会发生什么?有点像“哦,当然,64k就足够了”(B.G.),或者“千年虫”。没有意义。@Fred ii-:Unix时间戳基本上是自1970-01-01以来经过的秒数,有一个最大整数值。自1970年以来,该值在32位(2^32-1)秒内为2038-01-19。@MadaraUchiha谢谢。那么接下来会发生什么,会有解决办法吗?
<?php
$date = "27.05.2045";
$hour = "22";
$min = "15";
// 1. This one works
$datetime = DateTime::createFromFormat('d.m.Y H:i', $date.' '.$hour.':'.$min);
$now = new DateTime();
if ($datetime < $now)
{
echo "Datetime is in the past";
}
else if ($datetime > $now)
{
echo "Datetime is in the future";
}
// 2. Does not work
if (time() > strtotime($date.' '.$hour.':'.$min))
{
echo "Datetime is in the past (strtotime)";
}
else if (time() < strtotime($date.' '.$hour.':'.$min))
{
echo "Datetime is in the future (strtotime)";
}
// 3. Using another date format but still does not work
$array = explode('.', $date);
$date_converted = $array[2].'-'.$array[1].'-'.$array[0];
if (time() > strtotime($date_converted.' '.$hour.':'.$min))
{
echo "Datetime is in the past (strtotime with converted date)";
}
else if (time() < strtotime($date_converted.' '.$hour.':'.$min))
{
echo "Datetime is in the future (strtotime with converted date)";
}
?>