PHP将时间与未定义的时区进行比较

问题描述

|| 我需要比较时间,我正在使用变量定义时区,当前时间和要比较的时间。 我正在使用此脚本根据用户的选择执行某些操作。用户可以选择执行此操作的时间,日期和时区。 如果当前时间大于或等于用户选择的时间,则应执行这些操作。 所以我需要比较当前时间和用户选择的时间。当前时间由用户选择的时区给出。 这是我的代码。比较效果不佳。它认为相反的事情比实际应该想到的...我需要帮助..可能不使用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;

}
    

解决方法

        如果用户在数据库中定义的时间是相对于同一时区的。您可以尝试以下代码片段:
$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时间戳保存用户定义的时间。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...