【问题标题】:How to determine if a datetime has passed or not for Sydney Time如何确定悉尼时间的日期时间是否已过
【发布时间】: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 你什么意思?可以给我举个例子吗?

标签: php date time strtotime


【解决方案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)

【讨论】:

    猜你喜欢
    • 2012-07-01
    • 1970-01-01
    • 2017-02-20
    • 2021-06-20
    • 1970-01-01
    • 2011-02-19
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多