Back in the dim dark past when Apple was trying to sell computer systems with only one mouse button, PHP developers used the
date function to display Unix times. Today you have to between choose
The Unix time is recorded as the number of seconds since January 1, 1970. Using the current 32 bit integer format, Unix and Linux systems will crash in 2038. The design error is called the Y2038 bug. If the Unix/Linux developers switch to 64 bit integers, they will survive 2038 but will never venture into the past. The Unix time format is only useful for limited short term timestamps on temporary data.
Unfortunately applications use the Unix time for serious work and you have to learn to display the Unix time. The current date and time, using the Unix timestamp displayed through the
date function, is 2013-05-26 11:00:40.
But here is the catch. In the old days when people purchased mobile phones to make telephone calls, date returned the exact date and time stored on your computer. Today date returns the date and time changed to whatever PHP thinks is correct for your timezone. You have to tell PHP the right timezone and you have to make sure your operating system has up to date information on timezone changes for your time zone.
There are now strange effects from using date. When you move the clock forward for summer time, you have a gap in all your logs. When you move the clock back for winter time, there is an overlap of an hour or more. There are many other problems.
How do most systems tackle the strange errors? They record the GMT date, Greenwich Mean Time, which does not change. They leave any time zone conversions to the time of display, which may be after you have updated your computer for time zone changes.
The following table shows some examples of
gmdate. The date() column shows the time displayed in the form
print date('Y-m-d H:i:s'); which produces 2013-05-26 11:00:40. The gmdate() column shows the time displayed in the form
print gmdate('Y-m-d H:i:s'); which produces 2013-05-26 01:00:40.
There are times when you want to record only a time and you want to record the exact time. You can record times without dates then display them using date and gmdate. Times are manually supplied to date and gmdate. A time of 30seconds is specified in the form
<?php print gmdate('H:i:s', 30); ?> which produces 00:00:30. date,
<?php print date('H:i:s', 30); ?>, produces 10:00:30 for the Sydney time zone.
If you display the year, you get 1970, as shown by
print date('Y-m-d H:i:s', 0);, which produces 1970-01-01 10:00:00. Times stored in this format are useful only for the time, not the date.
Notice the difference between the date and gmdate functions in the following list. For time values, gmdate reports the correct time while date adds what PHP thinks is the difference between GMT and your local time.
|Date and format||date()||gmdate()|
|2013-05-26 11:00:40||2013-05-26 01:00:40|
|1970-01-01 10:00:30||1970-01-01 00:00:30|
You can get the time zone using
print date_default_timezone_get(); and it produces Australia/Sydney.
You can set the time zone using
print date_default_timezone_set('GMT');. GMT sets the universal time with no time zone adjustments. UTC is the American version of GMT and started in the American navy.
If you set your time zone to GMT, all your elapsed time recordings will be accurate but you will not be able to display the time of day for your time zone. If you leave your time zone set for your time zone, you can display times without the interference from time zones and that is why you have gmdate.
As an example, you want to record and display your times for running marathons. You want your times to appear as 3:27:05, not 13:27:05. You display them using gmdate instead of date.
What about a marathon that is to be shown live on television? You want your friends to see it at the correct time in their time zone. You ask them to register at your site and set the time zone in their profile. You display the time using date. When you enter your local start time of 08:30:00, they will see the time adjusted to their time zone. If you take this approach, you also have to display the date because their time zone might be in the previous day or in the subsequent day.
What about displaying your schedule for running the 8:30 marathon including warm up and cool down times? You want the times displayed in your time zone and not adjusted for anything else. Enter the times as GMT times and display the times using gmdate.