Reputation: 5818
My php file is hosted in some other part of the world. The date()
and time()
functions returns the date/time on the server. How do I convert that date so that it's the same as my local date/time?
The date on the server is 10 hours behind my local time. I could just hard code and substract this from the server time. But what is the proper way of going about this so that no value has to be hardcoded?
Upvotes: 3
Views: 5598
Reputation: 9122
time()
does not return "time on the server", i.e., it returns UNIX-timestamp which has nothing to do with local time zone (it always uses GMT as it's time zone). date()
formats such UNIX-timestamp to a user-readable format, based on server's time zone. You can set custom time zone with date_default_timezone_set()
(put it in your index.php or config.php). For lists of supported time zones, take a look at http://php.net/manual/en/timezones.php.
Basically, you can use time() for each and every time zone (it uses fixed time zone, so it's value will be the same in "different time zones"), and date() in conjunction with date_default_timezone_set() for printing date/time.
Upvotes: 5
Reputation: 1548
can use this function date_default_timezone_set
date_default_timezone_set('Europe/Zurich');
That will get it i think, here is a list of timezones
Hope this helps :)
Upvotes: 9