我正在尝试编写一个简单的脚本。从本质上讲,它所要做的就是告诉我提供的日期($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;
}
}
?>
答案 0 :(得分:1)
$oz = new DateTimezone('Australia/Sydney');
$now = new DateTime('now', $oz);
$dateToCheck = new DateTime("2019-06-08T18:00:00", $oz);
var_dump($now, $dateToCheck, $now <=> $dateToCheck);
输出:
object(DateTime)#2 (3) {
["date"]=>
string(26) "2019-06-08 09:25:25.623080"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Australia/Sydney"
}
object(DateTime)#3 (3) {
["date"]=>
string(26) "2019-06-08 18:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Australia/Sydney"
}
int(-1)