【发布时间】:2019-06-07 22:36:20
【问题描述】:
我正在尝试制作一个简单的脚本。基本上它所做的就是告诉我我提供的日期 ($dateToCheck) 是否已经过去或将来还没有到来。该程序似乎可以正常工作,但问题是日期/时间没有转换为悉尼时间。我希望所有日期都在悉尼时间。因此,如果$dateToCheck 和当前时间之间存在巨大的时间间隔,则该脚本可以工作。但是,如果当前日期和$dateToCheck 仅相隔几个小时,它就会停止给出正确答案。因此,问题在于时区。
<?php
// All dates and time are in Sydney time
date_default_timezone_set('Australia/Sydney');
$currTime = time();
$dateToCheck = "2019-06-08T18:00:00"; // 10th June 2018 at 7:00pm
$answer = has_date_past($currTime, $dateToCheck);
echo $answer;
// If the date has passed the current time, return true
function has_date_past($currTime, $date) {
$checkDate = strtotime($date);
echo $checkDate . "<br>";
echo $currTime . "<br>";
if ($currTime <= $checkDate) {
return 1;
} else {
return 0;
}
}
?>
【问题讨论】:
-
DateTime对象可以使用标准比较运算符进行比较,并具有内置的时区支持。 -
@Sammitch 你什么意思?可以给我举个例子吗?