DiegoP.
DiegoP.

Reputation: 45737

PHP compare time with undefined Timezones

I need to compare times, I am using variables to 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;

}

Upvotes: 0

Views: 1107

Answers (1)

silverfox
silverfox

Reputation: 5272

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.

Upvotes: 1

Related Questions