PHP, C#, JavaScript, etc... software develoment

Date, time and time zone: non obvious about obvious

int time(void)

"Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)."

That is obvious, yes, exactly.
Then please answer on easy question... Is my time() and your time() return the same values right now?

YES

They are the same., for example if my current time is "Fri, 21 Jan 11 09:06:42 -0600" and time() returns "1295622402" then right now your time() will return the same value "1295622402" but your current time is "Fri, 21 Jan 11 17:06:40 +0200" (for example). Why current time is different?, because different time zones, my time zone is -0600 and your time zone is +0200.

I can change time zone with date_default_timezone_set (or any another way) and time() will return the same value but current time will be changed. For example
date_default_timezone_set("Europe/London"); echo date(DATE_RFC822, "1295622402");
and get "Fri, 21 Jan 11 15:06:40 +0000" - different current time value but time() is the same because it is absolute, it is the number of seconds since the Unix Epoch.

date(...) and mktime(....) are time zone dependent.
based on set time zone with some php function or in the php.ini they will return different values because time zone is used to convert to/from unix time stamp.
date(...) is used to convert from unix time stamp using some format.
mktime(....) is used to convert numbers to unix time stamp.

Developing distributed application we need to synchronize data between two databases. We do not think about date/time problem if our application distributed in the one time zone, but if does not then.... then everything is simple. Need just keep time() value in the database when some action is happened. My time is "9:06 am -0600", I create some instance USER1 in the my database and save time() value as time creation "USER1.created_at". Then synchronize my database with your to you get my USER1 instance. In your database "USER1.created_at" has the same value as in the mine and date(DATE_RFC822, "USER1.created_at") returns "17:06 +0200". It is true, I have created USER1 at "9:06 am -0600" but for you it was created "17:06 +0200".

Everything is simple if keep time() value in the database as "created_at" field value.

That's it.

Contact universal@vitana-group.com if you have any questions or found a bug.

Copyright © 2005 - Vitana