【问题标题】:PHP compare time with undefined TimezonesPHP 将时间与未定义的时区进行比较
【发布时间】:2011-05-28 00:36:05
【问题描述】:

我需要比较时间,我正在使用变量来定义时区、当前时间和要比较的时间。

我正在使用此脚本根据用户的选择执行某些操作。用户可以选择执行此操作的时间、日期和时区。

如果当前时间 > 或等于用户选择的时间,则该操作应运行。

所以我需要比较当前时间和用户选择的时间。当前时间由用户选择的时区给出。

这是我的代码。比较不能正常工作。它认为与实际认为相反的事情...我需要帮助..可能不使用 php 5.3 或更高版本。如果功能可以从版本 4 到 5 就更好了

date_default_timezone_set($TimeZone); //all the possible php timezones available can be choosen by the user
$todaydate = date('Y-m-d');
$time_now=mktime(date('G'),date('i'),date('s'));
$NowisTime=date('G:i:s',$time_now); //the time given by the selected timezone above
$time = $ris['Time']; //the time defined by the user in the DB
if($NowisTime >= $time){
DO THE ACTIONS
}else{
exit;

}

【问题讨论】:

    标签: php time compare


    【解决方案1】:

    如果数据库中用户定义的时间是相对于同一时区的。你可以试试这个 sn-p 跟随:

    $dateTimeZone = new DateTimeZone("Asia/Chongqing");
    $localTime = new DateTime("now", $dateTimeZone);
    $definedTime = new DateTime("2011-05-29 10:00:00", $dateTimeZone);
    if ($localTime >= $definedTime) {
        echo "It is about time.";
    } else {
        // do nothing.
    }
    

    使用Unix Time Stamp 来节省用户定义的时间会更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多