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:
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.
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.
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.
@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
@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).
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!
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