PHP compare time with undefined Timezones
I need to compare times, I am using variables t开发者_StackOverflowo define timezones, the current time and the time to be compared.
I am using this script to do certain actions based on what the user selects. The user can select a time when this action should be done, a date and a timezone.
If the current time is > or equal than the time selected by the user those action should run.
So I need to compare the current time and the time selected by the user. The current time is given by the timezone selected by the user.
This is the code I have. The comparison is not working fine. It thinks the oppposite thing than what should actually thinks... I need an help.. possibly not using php 5.3 or above. Better if functions are avaialbe from version 4 to 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;
}
If user defined time in database is relative to same timezone. you could try this snippet follow:
$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.
}
It would be better to save user defined time with Unix Time Stamp.
精彩评论