Skip to main content
Inspiring
August 30, 2008
Answered

Timezones in php

  • August 30, 2008
  • 3 replies
  • 450 views
I need some advice... obviously..

I have a multi-timezone php site (MySQL) and have been offered lots of ways to approach this.

My current solution is to use a putenv() function in the header to change the timezone environment.

i.e. putenv ("TZ=".$_SESSION['usertimezone']);

This works great when using echo to adjust a timestamp (e.g. Time())
using echo date("D, d-m-Y H:i:s",$whatevertimestamp);

BUT... it gets more complex when entering dates into a database and returning them from it in the correct timezone.

I am sort of aware of the date_default_timezone_set() function but am not clear what the differences are.

I have also heard about the new DateTime($timefield, new DateTimeZone($usertimezone)); function which could convert a date/time field into a timestamp and associate a timezone.

Am I even going in the right direction??????

Any help on this would be very appreciated.

This topic has been closed for replies.
Correct answer RichardODreamweaver
OK - finally solved it and it was so simple...

Provided the putenv("TZ"=..... is defined, you can use

strtotime, which uses the dat/time string and converts it using the default system timezone (which has been defined). The converted UTC epoch value can then be stored.

When another user logs in and his/her timezone is different, you can take the UTC epoch value from the database and parse it using a simple date() function.

This converts the UTC date into a local date!!!

So simple when you know how.....

3 replies

RichardODreamweaverAuthorCorrect answer
Inspiring
September 13, 2008
OK - finally solved it and it was so simple...

Provided the putenv("TZ"=..... is defined, you can use

strtotime, which uses the dat/time string and converts it using the default system timezone (which has been defined). The converted UTC epoch value can then be stored.

When another user logs in and his/her timezone is different, you can take the UTC epoch value from the database and parse it using a simple date() function.

This converts the UTC date into a local date!!!

So simple when you know how.....
Inspiring
September 2, 2008
Thanks zerof

I have been through these in detail but, being new to the subject, it was quite complex!

The Laughingmeme site was the most useful and I have got as far as being able to create a string incorporating the timezone difference (formatted in DATE_RFC3339 format)

I can now presumably store this value into a VARCHAR table field.

My problem now is echoing in the different timezone.

e.g.

If I use "2008-09-02 21:10:15" as entered by the datetime picker, I can use

new DateTime($_POST['timefield'], new DateTimeZone(GB));

which will return

2008-09-02T21:10:15+01:00

Great - insert or update it!

My issue now is when user 2 in UTC timezone accesses this value, it didn't do what I wanted which was to account for the "+01:00" value.

Do I have to use a sttotime function here or another object to calculate the new value?

David, Micha, anyone - this is really annoying me now - I've been on this subject for over a week and not really got much further!
Inspiring
August 30, 2008
RichardODreamweaver escreveu:
> I need some advice... obviously..
>
> I have a multi-timezone php site (MySQL) and have been offered lots of ways to
> approach this.
>
> My current solution is to use a putenv() function in the header to change the
> timezone environment.
>
> i.e. putenv ("TZ=".$_SESSION['usertimezone']);
>
> This works great when using echo to adjust a timestamp (e.g. Time())
> using echo date("D, d-m-Y H:i:s",$whatevertimestamp);
>
> BUT... it gets more complex when entering dates into a database and returning
> them from it in the correct timezone.
>
> I am sort of aware of the date_default_timezone_set() function but am not
> clear what the differences are.
>
> I have also heard about the new DateTime($timefield, new
> DateTimeZone($usertimezone)); function which could convert a date/time field
> into a timestamp and associate a timezone.
>
> Am I even going in the right direction??????
>
> Any help on this would be very appreciated.
---------------
PHP has some useful functions to get and set Timezones:

timezone_identifiers_list
timezone_abbreviations_list
date_default_timezone_get
date_default_timezone_set
timezone_name_from_abbr
timezone_name_get
timezone_open
timezone_transitions_get

http://www.educar.pro.br/i_en/php/dtt/

and the internal DateTime and DateTimeZone

http://laughingmeme.org/2007/02/27/looking-at-php5s-datetime-and-datetimezone/

http://laughingmeme.org/tag/timezone/

http://hessianphp.sourceforge.net/index.php?n=UserGuide.DateTimeConfig

----
zerof