Comparing date+time+timezone

I’m working on a new feature for my free poll website. Members can enter the expiration date+time+timezone for their poll. First thing that I do is check if the given date+time is in the past. I use the following code to do this:

if (mktime($_POST[‘hour’],$_POST[‘min’],0,$_POST[‘month’],$_POST[‘day’],$_POST[‘year’]) - (60 * 60 * $_POST[‘timezone’] / 100) < mktime(date(“H”),date(“i”),0,date(“m”),date(“d”),date(“Y”)) - (60 * 60 * $system_timezone / 100)) {…error…}

But the code doesn’t seem to work.
$_POST[‘hour’],$_POST[‘min’],$_POST[‘month’],$_POST[‘day’],$_POST[‘year’],$_POST[‘timezone’] are input from the user.

$system_timezone is the timezone of my server

why don’t you debug it some?
did you check returned values?
what does mean this strange formula 60 * 60 * $system_timezone / 100?

You usinng the timezone wrong. This is a very common mistake for many php programmers.
First of all, when user enter a timezone in the form, what format is it in? Is it in minutes/seconds or in the timezone name like EST?

The proper way to use the timezone is to use the timezone name, then let php adjust the time and calculate the time difference based on a timezone.

The best way to do it is to use php 5.3 and it has great class called DateInterval http://us2.php.net/manual/en/class.dateinterval.php

You may also need this class:
http://us2.php.net/manual/en/datetimezone.getoffset.php

This only requires php 5.2

Anyway, you really need to use the php functions that specifically designed to calculate time difference between 2 times taking timezones into account. Manually adjusting for timezone is an old way of doing this, from the php 4 days.

I am not quite getting the use of timezone here so but apart from the timezone I would do something like this for comparision:


if(checkdate($_POST['month'],$_POST['day'],$_POST['year'])){
	$selected_date = $_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'] . ' ' . $_POST['hour'] . ':' . $_POST['min'] . ':0';
	$today = date('Y-m-d') . ' ' . $_POST['hour'] . ':' . $_POST['min'] . ':0';
	if(strtotime($selected_date) < strtotime($today)){
		// through error message
	}
}

Thanks for your input guys.

@lampcms.com
I don’t even know which PHP version I have! I leave this to my system administrator. So I will try to solve this the old way. Also, now I really want to know why my solutions doesn’t seem to work :wink:

@rajug
I need the timezone here because someone may want to let their poll expire on Jan 10, 2010 at 4pm in Australia, and another may want it to expire on Jan 10, 2010 at 4pm in Holland. Both dates and times are the same but the polls expire at different times because of the timezone.

@Shrapnel_N5
I think the problem is “60 * 60 * $timezone / 100”. I think it should be “60 * 60 * $timezone”. A timezone of -1 means a time difference of 1 hour with the GMT standard time. The function mktime() measures in seconds. So when I convert the timezone from hours to seconds I get: 60 (sec) x 60 (min) x $timezone (hour).

BTW what are you storing in the database for timezone? Is it timezone string or something else? If you are storing them for a user (who creates the poll) then you can use [URL=“http://www.php.net/manual/en/function.date-default-timezone-set.php”]date_default_timezone_set.

Hope this way you can achieve the goal.

I used the following values to store the timezones: Timezone List. This means that I had them stored as type double (like +5.75). That’s why my code didn’t work with the “/100”. But now I realise that it’s better to store the timezone values as interger (5.75 will be stored as 575) and use the “/100” in my code above.

I store the timezone for each poll. I don’t think that the function date_default_timezone_set will help me here because it sets a default timezone used by all date/time functions in the script whereas I want to calculate the difference between 2 dates/times taking into account the timezone.

I mean you can set the default timezone before you get the date to store in the database while saving the poll data. Setting the default timezone will be for a particular script. So there will not be problem. Since I am not sure storing the integer value like you said and calculate dates for correct, I would go for storing timezone strings. Anyway good luck!

There are a wide variety of date functions in PHP.

One that might be suitable for you is date_create

[DateTime date_create ([ string $time = “now” , [url=“http://www.php.net/manual/en/class.datetimezone.php”]DateTimeZone $timezone = NULL ]] )

In PHP, timezones are not used as static numbers - they are instead used as region/location. For example: ‘Europe/Prague’
See list of supported timezones


Paul Wilkins