PHP Dates and Times Introduction - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Dates and Times Introduction - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Friday, May 10, 2019

PHP Dates and Times Introduction

Dates and Times



Introduction


Displaying and manipulating dates and times seems simple at first but gets more difficult depending on how diverse your users are. Do your users span more than one time zone? Probably so, unless you are building an intranet or a site with a very specific geographical audience. Is your audience frightened away by timestamps that look like “2015-07-20 14:56:34 EDT” or do they need to be calmed with familiar representations like “Saturday July 20, 2015 (2:56 P.M.)”? Calculating the number of hours between today at 10 A.M. and today at 7 P.M. is pretty easy. How about between today at 3 A.M. and noon on the first day of next month? 

These calculations and manipulations are made even more hectic by daylight saving (or summer) time (DST). Because of DST, there are times that don’t exist (in most of the United States, 2 A.M. to 3 A.M. on a day in the spring) and times that exist twice (in most of the United States, 1 A.M. to 2 A.M. on a day in the fall). Some of your users may live in places that observe DST, some may not. 

Programmatic time handling is made much easier by two conventions. First, treat time internally as Coordinated Universal Time (abbreviated UTC and also known as GMT, Greenwich Mean Time), the patriarch of the time-zone family with no DST or summer time observance. This is the time zone at 0 degrees longitude, and all other time zones are expressed as offsets (either positive or negative) from it. Second, treat time not as an array of different values for month, day, year, minute, second, etc., but as seconds elapsed since the Unix epoch: midnight on January 1, 1970 (UTC, of course). This makes calculating intervals much easier, and PHP has plenty of functions to help you move easily between epoch timestamps and human-readable time representations.

The function mktime() produces epoch timestamps from a given set of time parts, while date(), given an epoch timestamp, returns a formatted time string. Example uses these functions to find on what day of the week New Year’s Day 1986 occurred.

Example   Using mktime() and date()

             $stamp = mktime(0,0,0,1,1,1986);
             print date('l',$stamp);


Example 3-1 prints:

             Wednesday


In Example mktime() returns the epoch timestamp at midnight on January 1, 1986. The l format character to date() tells it to return the full name of the day of the week that corresponds to the given epoch timestamp. 

To ensure smooth date and time processing in your code, set the date.timezone configuration variable to an appropriate time zone (or call date_default_time zone_set() before you do any date or time operations). To always use UTC as the time zone for your date calculations, set date.timezone to UTC. You can ensure a time or date is represented in a way appropriate to a user’stime zone and location at display time.

In this book, the phrase epoch timestamp refers to a count of seconds since the Unix epoch. Time parts (or date parts or time and date parts) means an array or group of timeand date components such as day, month, year, hour, minute, and second. Formatted time string (or formatted date string, etc.) means a string that contains some particular grouping of time and date parts—for example, “2002-03-12,” “Wednesday, 11:23 A.M.,” or “February 25.”

If you used epoch timestamps as your internal time representation, you avoided any Y2K issues, because the difference between 946702799 (1999-12-31 23:59:59 UTC) and 946702800 (2000-01-01 00:00:00 UTC) is treated just like the difference between any other two timestamps. You may, however, run into a Y2038 problem. January 19, 2038 at 3:14:07 A.M. (UTC) is 2147483647 seconds after midnight January 1, 1970. What’s special about 2147483647? It’s 231 − 1, which is the largest integer expressible when 32 bits represent a signed integer. (The 32nd bit is used for the sign.)

The PHP functions that rely on its bundled time handling library, such as date(), mktime(), and the methods of the DateTime class store timestamps internally as 64-bit integers. This gives you about a 600-billion year range, which is probably adequate for your calculations. For this reason, as well as simplicity, this chapter uses those functions for date and time operations instead of functions such as strftime() and gmstrftime(). These functions rely on underlying system calls, which may not have the same range or functionality.

No comments:

Post a Comment

Post Top Ad