I have a server which is set to EST, and all records in the database are set to EST. I would like to know how to set it to GMT. I want to offer a time zone option to my users.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
A lot of time there is task when necessary record time in DB in GMT+0, but function time() will return time of the server. Its possible resolve by next function:
I would strongly suggest avoiding messing with UNIX timestamps to make it look like a different time zone. This is a lesson I've learnt the hard way, way too many times.
A timestamp is the number of seconds since Midnight 1 January 1970, GMT. It doesn't matter where you are in the world, a given timestamp represents the exact same moment in time, regardless of time zones. Yes, the timestamp "0" means 10am 1/1/70 in Australia, Midnight 1/1/70 in London and 5pm 31/12/69 in LA, but this doesn't mean you can simply add or subtract values and guarantee accuracy.
The golden example which stuffed me up every year for way too long was when daylight savings would crop up. Daylight savings means that there are "clock times" which don't exist in certain parts of the world. For example, in most parts of the US, there was no such time as 2:01am on April 2, 2006.
Enough quasi-intelligible ranting though. My advice is to store your dates in the database as timestamps, and then...
date()
gmdate()
date_default_timezone_set()
and thendate()
For example,
This will keep your values clean (you don't have to be worrying about adding offsets, and then subtracting before saving), it will respect all Daylight Savings rules, and you also don't have to even look up the timezone of the place you want.
Here is a list of the time zones with different from GMT, hope this is helpful!
Thanks!
It is important to note that if you are using date('Z') to convert a date that is stored in a database that you give the date the timestamp as the second argument, otherwise your result will not be accurate.
e.g.
In the UK, sometimes date('Z') will give 3600, sometimes it will give 0.
Gives 0
Gives 3600
So purely using strtotime($dbDateValue) - date('Z') can be wrong
Instead use:
This of course relies on PHP and the database both being set to the same timezone.
I was inspired by this question to write an alternative function to convert a timestamp to a needed timezone.
So, CORRECT procedure steps to record, display (convert) time:
Results:
$date_input = 2012-08-17 12:00:00
$date_input = 2012-02-17 12:00:00
I decided to getLTime_nonclass hoping to make the conversion faster than using classes. All I ask is to confirm that getLTime_nonclass is a valid replacement for getLTime_class. Please feel free to express your opinion. Thanks
No matter in which GMT time zone the server is, here is an extremely easy way to get time and date for any time zone. This is done with the
time()
andgmdate()
functions. Thegmdate()
function normally gives us GMT time, but by doing a trick with thetime()
function we can get GMT+N or GMT-N, meaning we can get the time for any GMT time zone.For example, if you have to get the time for GMT+5, you can do it as follows
Now if you have to get the time for GMT-5, you can just subtract the offset from the
time()
instead of adding to it, like in the following example where we are getting the time for GMT-4