<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://commons.oreilly.com/wiki/skins/common/feed.css?97"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>PHP Cookbook/Dates and Times - Revision history</title>
		<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Dates_and_Times&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.11.0</generator>
		<lastBuildDate>Thu, 23 May 2013 01:50:49 GMT</lastBuildDate>
		<item>
			<title>Docbook2Wiki: Initial conversion from Docbook</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Dates_and_Times&amp;diff=7315&amp;oldid=prev</link>
			<description>&lt;p&gt;Initial conversion from Docbook&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 13:36, 7 March 2008&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;</description>
			<pubDate>Fri, 07 Mar 2008 13:36:04 GMT</pubDate>			<dc:creator>Docbook2Wiki</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:PHP_Cookbook/Dates_and_Times</comments>		</item>
		<item>
			<title>Evanlenz: 1 revision(s)</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Dates_and_Times&amp;diff=3109&amp;oldid=prev</link>
			<description>&lt;p&gt;1 revision(s)&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 22:29, 6 March 2008&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;</description>
			<pubDate>Thu, 06 Mar 2008 22:29:58 GMT</pubDate>			<dc:creator>Evanlenz</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:PHP_Cookbook/Dates_and_Times</comments>		</item>
		<item>
			<title>Docbook2Wiki: Initial conversion from Docbook</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Dates_and_Times&amp;diff=3108&amp;oldid=prev</link>
			<description>&lt;p&gt;Initial conversion from Docbook&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{PHP Cookbook/TOC}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Displaying and manipulating dates and times seems simple at first but gets more difficult depending on how diverse and complicated 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 &amp;quot;2002-07-20 14:56:34 EDT&amp;quot; or do they need to be calmed with familiar representations like &amp;quot;Saturday July 20, 2000 (2:56 P.M.).&amp;quot; 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? Finding the difference between dates is discussed in [[PHP Cookbook/Dates and Times#Finding the Difference of Two Dates|Recipe 3.6]] and [[PHP Cookbook/Dates and Times#Finding the Difference of Two Dates with Julian Days|Recipe 3.7]].&lt;br /&gt;
&lt;br /&gt;
These calculations and manipulations are made even more hectic by daylight saving (or summer) time (DST). Thanks to DST, there are times that don't exist (in most of the United States, 2 A.M. to 3 A.M. on the first Sunday in April) and times that exist twice (in most of the United States, 1 A.M. to 2 A.M. on the last Sunday in October). Some of your users may live in places that observe DST, some may not. [[PHP Cookbook/Dates and Times#Calculating Time with Time Zones|Recipe 3.12]] and [[PHP Cookbook/Dates and Times#Accounting for Daylight Saving Time|Recipe 3.13]] provide ways to work with time zones and DST.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
The function &amp;lt;tt&amp;gt;mktime( )&amp;lt;/tt&amp;gt; produces epoch timestamps from a given set of time parts, while &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; , given an epoch timestamp, returns a formatted time string. You can use these functions, for example, to find on what day of the week New Year's Day 1986 occurred:&lt;br /&gt;
&lt;br /&gt;
 $stamp = mktime(0,0,0,1,1,1986);&lt;br /&gt;
 print date('l',$stamp);&lt;br /&gt;
 '''Wednesday'''&lt;br /&gt;
          &lt;br /&gt;
&lt;br /&gt;
This use of &amp;lt;tt&amp;gt;mktime( )&amp;lt;/tt&amp;gt; returns the epoch timestamp at midnight on January 1, 1986. The &amp;lt;tt&amp;gt;l&amp;lt;/tt&amp;gt; format character to &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; tells it to return the full name of the day of the week that corresponds to the given epoch timestamp. [[PHP Cookbook/Dates and Times#Printing a Date or Time in a Specified Format|Recipe 3.5]] details the many format characters available to &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
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 time and 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 &amp;quot;2002-03-12,&amp;quot; &amp;quot;Wednesday, 11:23 A.M.,&amp;quot; or &amp;quot;February 25.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
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 2&amp;lt;sup&amp;gt;31&amp;lt;/sup&amp;gt; - 1, which is the largest integer expressible when 32 bits represent a signed integer. (The 32nd bit is used for the sign.)&lt;br /&gt;
&lt;br /&gt;
The solution? At some point before January 19, 2038, make sure you trade up to hardware that uses, say, a 64-bit quantity for time storage. This buys you about another 292 billion years. (Just 39 bits would be enough to last you until about 10680, well after the impact of the Y10K bug has leveled the Earth's cold fusion factories and faster-than-light travel stations.) 2038 might seem far off right now, but so did 2000 to COBOL programmers in the 1950s and 1960s. Don't repeat their mistake!&lt;br /&gt;
&lt;br /&gt;
== Finding the Current Date and Time ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to know what the time or date is.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; for a formatted time string:&lt;br /&gt;
&lt;br /&gt;
 print strftime('%c');&lt;br /&gt;
 print date('r');&lt;br /&gt;
 '''Mon Aug 12 18:23:45 2002'''&lt;br /&gt;
                '''Mon, 12 Aug 2002 18:23:45 -0400'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;getdate( )&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;localtime( )&amp;lt;/tt&amp;gt; if you want time parts:&lt;br /&gt;
&lt;br /&gt;
 $now_1 = getdate( );&lt;br /&gt;
 $now_2 = localtime( );&lt;br /&gt;
 print &amp;quot;$now_1[hours]:$now_1[minutes]:$now_1[seconds]&amp;quot;;&lt;br /&gt;
 print &amp;quot;$now_2[2]:$now_2[1]:$now_2[0]&amp;quot;;&lt;br /&gt;
 '''18:23:45'''&lt;br /&gt;
                '''18:23:45'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The functions &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; can produce a variety of formatted time and date strings. They are discussed in more detail in [[PHP Cookbook/Dates and Times#Printing a Date or Time in a Specified Format|Recipe 3.5]]. Both &amp;lt;tt&amp;gt;localtime( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;getdate( )&amp;lt;/tt&amp;gt;, on the other hand, return arrays whose elements are the different pieces of the specified date and time.&lt;br /&gt;
&lt;br /&gt;
The associative array &amp;lt;tt&amp;gt;getdate( )&amp;lt;/tt&amp;gt; returns has the key/value pairs listed in [[PHP Cookbook/Dates and Times#phpckbk-CHP-3-TABLE-1|Table 3-1]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-3-TABLE-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-1. Return array from getdate( )'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Key !! Value&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;seconds&amp;lt;/tt&amp;gt; || Seconds&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;minutes&amp;lt;/tt&amp;gt; || Minutes&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;hours&amp;lt;/tt&amp;gt; || Hours&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;mday&amp;lt;/tt&amp;gt; || Day of the month&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;wday&amp;lt;/tt&amp;gt; || Day of the week, numeric (Sunday is 0, Saturday is 6)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;mon&amp;lt;/tt&amp;gt; || Month, numeric&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;year&amp;lt;/tt&amp;gt; || Year, numeric&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;yday&amp;lt;/tt&amp;gt; || Day of the year, numeric (e.g., 299)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;weekday&amp;lt;/tt&amp;gt; || Day of the week, textual, full (e.g., &amp;quot;Friday&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;month&amp;lt;/tt&amp;gt; || Month, textual, full (e.g., &amp;quot;January&amp;quot;)&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example, here's how to use &amp;lt;tt&amp;gt;getdate( )&amp;lt;/tt&amp;gt; to print out the month, day, and year:&lt;br /&gt;
&lt;br /&gt;
 $a = getdate( );&lt;br /&gt;
 printf('%s %d, %d',$a['month'],$a['mday'],$a['year']);&lt;br /&gt;
 '''August 7, 2002'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
Pass &amp;lt;tt&amp;gt;getdate( )&amp;lt;/tt&amp;gt; an epoch timestamp as an argument to make the returned array the appropriate values for local time at that timestamp. For example, the month, day, and year at epoch timestamp 163727100 is:&lt;br /&gt;
&lt;br /&gt;
 $a = getdate(163727100);&lt;br /&gt;
 printf('%s %d, %d',$a['month'],$a['mday'],$a['year']);&lt;br /&gt;
 '''March 10, 1975'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
The function &amp;lt;tt&amp;gt;localtime( )&amp;lt;/tt&amp;gt; returns an array of time and date parts. It also takes an epoch timestamp as an optional first argument, as well as a boolean as an optional second argument. If that second argument is &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;localtime( )&amp;lt;/tt&amp;gt; returns an associative array instead of a numerically indexed array. The keys of that array are the same as the members of the &amp;lt;tt&amp;gt;tm_struct&amp;lt;/tt&amp;gt; structure that the C function &amp;lt;tt&amp;gt;localtime( )&amp;lt;/tt&amp;gt; returns, as shown in [[PHP Cookbook/Dates and Times#phpckbk-CHP-3-TABLE-2|Table 3-2]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-3-TABLE-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-2. Return array from localtime( )'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Numeric position !! Key !! Value&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;tm_sec&amp;lt;/tt&amp;gt; || Second&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;tm_min&amp;lt;/tt&amp;gt; || Minutes&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;2&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;tm_hour&amp;lt;/tt&amp;gt; || Hour&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;tm_mday&amp;lt;/tt&amp;gt; || Day of the month&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;4&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;tm_mon&amp;lt;/tt&amp;gt; || Month of the year (January is 0)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;5&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;tm_year&amp;lt;/tt&amp;gt; || Years since 1900&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;6&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;tm_wday&amp;lt;/tt&amp;gt; || Day of the week&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;7&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;tm_yday&amp;lt;/tt&amp;gt; || Day of the year&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;8&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;tm_isdst&amp;lt;/tt&amp;gt; || Is daylight saving time in effect?&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example, here's how to use &amp;lt;tt&amp;gt;localtime( )&amp;lt;/tt&amp;gt; to print out today's date in month/day/year format:&lt;br /&gt;
&lt;br /&gt;
 $a = localtime( );&lt;br /&gt;
 $a[4] += 1;&lt;br /&gt;
 $a[5] += 1900;&lt;br /&gt;
 print &amp;quot;$a[4]/$a[3]/$a[5]&amp;quot;;&lt;br /&gt;
 '''8/7/2002'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
The month is incremented by 1 before printing since &amp;lt;tt&amp;gt;localtime( )&amp;lt;/tt&amp;gt; starts counting months with 0 for January, but we want to display &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt; if the current month is January. Similarly, the year is incremented by 1900 because &amp;lt;tt&amp;gt;localtime( )&amp;lt;/tt&amp;gt; starts counting years with 0 for 1900.&lt;br /&gt;
&lt;br /&gt;
Like &amp;lt;tt&amp;gt;getdate( )&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;localtime( )&amp;lt;/tt&amp;gt; accepts an epoch timestamp as an optional first argument to produce time parts for that timestamp:&lt;br /&gt;
&lt;br /&gt;
 $a = localtime(163727100);&lt;br /&gt;
 $a[4] += 1;&lt;br /&gt;
 $a[5] += 1900;&lt;br /&gt;
 print &amp;quot;$a[4]/$a[3]/$a[5]&amp;quot;;&lt;br /&gt;
 '''3/10/1975'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/strftime'', &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/date'', &amp;lt;tt&amp;gt;getdate( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/getdate'', and &amp;lt;tt&amp;gt;localtime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/localtime''.&lt;br /&gt;
&lt;br /&gt;
== Converting Time and Date Parts to an Epoch Timestamp ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to know what epoch timestamp corresponds to a set of time and date parts.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;mktime( )&amp;lt;/tt&amp;gt; if your time and date parts are in the local time zone:&lt;br /&gt;
&lt;br /&gt;
 // 7:45:03 PM on March 10, 1975, local time&lt;br /&gt;
 $then = mktime(19,45,3,3,10,1975);&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;gmmktime( )&amp;lt;/tt&amp;gt; if your time and date parts are in GMT:&lt;br /&gt;
&lt;br /&gt;
 // 7:45:03 PM on March 10, 1975, in GMT&lt;br /&gt;
 $then = gmmktime(19,45,3,3,10,1975);&lt;br /&gt;
&lt;br /&gt;
Pass no arguments to get the current date and time in the local or UTC time zone:&lt;br /&gt;
&lt;br /&gt;
 $now = mktime();&lt;br /&gt;
 $now_utc = gmmktime();&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The functions &amp;lt;tt&amp;gt;mktime( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;gmmktime( )&amp;lt;/tt&amp;gt; each take a date and time's parts (hour, minute, second, month, day, year, DST flag) and return the appropriate Unix epoch timestamp. The components are treated as local time by &amp;lt;tt&amp;gt;mktime( )&amp;lt;/tt&amp;gt;, while &amp;lt;tt&amp;gt;gmmktime( )&amp;lt;/tt&amp;gt; treats them as a date and time in UTC. For both functions, a seventh argument, the DST flag (1 if DST is being observed, 0 if not), is optional. These functions return sensible results only for times within the epoch. Most systems store epoch timestamps in a 32-bit signed integer, so &amp;quot;within the epoch&amp;quot; means between 8:45:51 P.M. December 13, 1901 UTC and 3:14:07 A.M. January 19, 2038 UTC.&lt;br /&gt;
&lt;br /&gt;
In the following example, &amp;lt;tt&amp;gt;$stamp_now&amp;lt;/tt&amp;gt; is the epoch timestamp when &amp;lt;tt&amp;gt;mktime( )&amp;lt;/tt&amp;gt; is called and &amp;lt;tt&amp;gt;$stamp_future&amp;lt;/tt&amp;gt; is the epoch timestamp for 3:25 P.M. on June 4, 2012:&lt;br /&gt;
&lt;br /&gt;
 $stamp_now = mktime( );&lt;br /&gt;
 $stamp_future = mktime(15,25,0,6,4,2012);&lt;br /&gt;
 &lt;br /&gt;
 print $stamp_now;&lt;br /&gt;
 print $stamp_future;&lt;br /&gt;
 '''1028782421'''&lt;br /&gt;
                '''1338837900'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
Both epoch timestamps can be fed back to &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; to produce formatted time strings:&lt;br /&gt;
&lt;br /&gt;
 print strftime('%c',$stamp_now);&lt;br /&gt;
 print strftime('%c',$stamp_future);&lt;br /&gt;
 '''Thu Aug  8 00:53:41 2002'''&lt;br /&gt;
                '''Mon Jun 4 15:25:00 2012'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
Because the previous calls to &amp;lt;tt&amp;gt;mktime( )&amp;lt;/tt&amp;gt; were made on a computer set to EDT (which is four hours behind GMT), using &amp;lt;tt&amp;gt;gmmktime( )&amp;lt;/tt&amp;gt; instead produces epoch timestamps that are 14400 seconds (four hours) smaller:&lt;br /&gt;
&lt;br /&gt;
 $stamp_now = gmmktime( );&lt;br /&gt;
 $stamp_future = gmmktime(15,25,0,6,4,2012);&lt;br /&gt;
 &lt;br /&gt;
 print $stamp_now;&lt;br /&gt;
 print $stamp_future;&lt;br /&gt;
 '''1028768021'''&lt;br /&gt;
                '''1338823500'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
Feeding these &amp;lt;tt&amp;gt;gmmktime( )&amp;lt;/tt&amp;gt;-generated epoch timestamps back to &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; produces formatting time strings that are also four hours earlier:&lt;br /&gt;
&lt;br /&gt;
 print strftime('%c',$stamp_now);&lt;br /&gt;
 print strftime('%c',$stamp_future);&lt;br /&gt;
 '''Wed Aug  7 20:53:41 2002'''&lt;br /&gt;
                '''Mon Jun 4 11:25:00 2012'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Dates and Times#Converting an Epoch Timestamp to Time and Date Parts|Recipe 3.4]] for how to convert an epoch timestamp back to time and date parts; documentation on &amp;lt;tt&amp;gt;mktime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/mktime'' and &amp;lt;tt&amp;gt;gmmktime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/gmmktime''.&lt;br /&gt;
&lt;br /&gt;
== Converting an Epoch Timestamp to Time and Date Parts ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want the set of time and date parts that corresponds to an epoch timestamp.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Pass an epoch timestamp to &amp;lt;tt&amp;gt;getdate( )&amp;lt;/tt&amp;gt; :&lt;br /&gt;
&lt;br /&gt;
 $time_parts = getdate(163727100);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The time parts returned by &amp;lt;tt&amp;gt;getdate( )&amp;lt;/tt&amp;gt; are detailed in [[PHP Cookbook/Dates and Times#phpckbk-CHP-3-TABLE-1|Table 3-1]]. These time parts are in local time. If you want time parts in another time zone corresponding to a particular epoch timestamp, see [[PHP Cookbook/Dates and Times#Calculating Time with Time Zones|Recipe 3.12]].&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Dates and Times#Converting Time and Date Parts to an Epoch Timestamp|Recipe 3.3]] for how to convert time and date parts back to epoch timestamps; [[PHP Cookbook/Dates and Times#Calculating Time with Time Zones|Recipe 3.12]] for how to deal with time zones; documentation on &amp;lt;tt&amp;gt;getdate( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/getdate''.&lt;br /&gt;
&lt;br /&gt;
== Printing a Date or Time in a Specified Format ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to print out a date or time formatted in a particular way.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 print strftime('%c');&lt;br /&gt;
 print date('m/d/Y');&lt;br /&gt;
 '''Tue Jul 30 11:31:08 2002'''&lt;br /&gt;
                '''07/30/2002'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Both &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; are flexible functions that can produce a formatted time string with a variety of components. The formatting characters for these functions are listed in [[PHP Cookbook/Dates and Times#phpckbk-CHP-3-TABLE-3|Table 3-3]]. The Windows column indicates whether the formatting character is supported by &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; on Windows systems.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-3-TABLE-3&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-3. strftime( ) and date( ) format characters'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Type !! strftime( ) !! date( ) !! Description !! Range !! Windows&lt;br /&gt;
|-&lt;br /&gt;
| Hour || &amp;lt;tt&amp;gt;%H&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;H&amp;lt;/tt&amp;gt; || Hour, numeric, 24-hour clock || 00-23 || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Hour || &amp;lt;tt&amp;gt;%I&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;h&amp;lt;/tt&amp;gt; || Hour, numeric, 12-hour clock || 01-12 || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Hour || &amp;lt;tt&amp;gt;%k&amp;lt;/tt&amp;gt; ||  || Hour, numeric, 24-hour clock, leading zero as space || 0-23 || No&lt;br /&gt;
|-&lt;br /&gt;
| Hour || &amp;lt;tt&amp;gt;%l&amp;lt;/tt&amp;gt; ||  || Hour, numeric, 12-hour clock, leading zero as space || 1-12 || No&lt;br /&gt;
|-&lt;br /&gt;
| Hour || &amp;lt;tt&amp;gt;%p&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;A&amp;lt;/tt&amp;gt; || AM or PM designation for current locale ||  || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Hour || &amp;lt;tt&amp;gt;%P&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; || am/pm designation for current locale ||  || No&lt;br /&gt;
|-&lt;br /&gt;
| Hour ||  || &amp;lt;tt&amp;gt;G&amp;lt;/tt&amp;gt; || Hour, numeric, 24-hour clock, leading zero trimmed || 0-23 || No&lt;br /&gt;
|-&lt;br /&gt;
| Hour ||  || &amp;lt;tt&amp;gt;g&amp;lt;/tt&amp;gt; || Hour, numeric, 12-hour clock, leading zero trimmed || 0-1 || No&lt;br /&gt;
|-&lt;br /&gt;
| Minute || &amp;lt;tt&amp;gt;%M&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;I&amp;lt;/tt&amp;gt; || Minute, numeric || 00-59 || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Second || &amp;lt;tt&amp;gt;%S&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;s&amp;lt;/tt&amp;gt; || Second, numeric || 00-61&amp;lt;ref&amp;gt;The range for seconds extends to 61 to account for leap seconds.&amp;lt;/ref&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Day || &amp;lt;tt&amp;gt;%d&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;d&amp;lt;/tt&amp;gt; || Day of the month, numeric || 01-31 || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Day || &amp;lt;tt&amp;gt;%e&amp;lt;/tt&amp;gt; ||  || Day of the month, numeric, leading zero as space || 1-31 || No&lt;br /&gt;
|-&lt;br /&gt;
| Day || &amp;lt;tt&amp;gt;%j&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;z&amp;lt;/tt&amp;gt; || Day of the year, numeric || 001-366 for &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt;; 0-365 for &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Day || &amp;lt;tt&amp;gt;%u&amp;lt;/tt&amp;gt; ||  || Day of the week, numeric (Monday is 1) || 1-7 || No&lt;br /&gt;
|-&lt;br /&gt;
| Day || &amp;lt;tt&amp;gt;%w&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;w&amp;lt;/tt&amp;gt; || Day of the week, numeric (Sunday is 0) || 0-6 || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Day ||  || &amp;lt;tt&amp;gt;j&amp;lt;/tt&amp;gt; || Day of the month, numeric, leading zero trimmed || 1-31 || No&lt;br /&gt;
|-&lt;br /&gt;
| Day ||  || &amp;lt;tt&amp;gt;S&amp;lt;/tt&amp;gt; || English ordinal suffix for day of the month, textual || &amp;quot;st,&amp;quot; &amp;quot;th,&amp;quot; &amp;quot;nd,&amp;quot; &amp;quot;rd&amp;quot; || No&lt;br /&gt;
|-&lt;br /&gt;
| Week || &amp;lt;tt&amp;gt;%a&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;D&amp;lt;/tt&amp;gt; || Abbreviated weekday name, text for current locale ||  || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Week || &amp;lt;tt&amp;gt;%A&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;l&amp;lt;/tt&amp;gt; || Full weekday name, text for current locale ||  || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Week || &amp;lt;tt&amp;gt;%U&amp;lt;/tt&amp;gt; ||  || Week number in the year; numeric; first Sunday is the first day of the first week || 00-53 || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Week || &amp;lt;tt&amp;gt;%V&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;W&amp;lt;/tt&amp;gt; || ISO 8601:1988 week number in the year; numeric; week 1 is the first week that has at least 4 days in the current year; Monday is the first day of the week || 01-53 || No&lt;br /&gt;
|-&lt;br /&gt;
| Week || &amp;lt;tt&amp;gt;%W&amp;lt;/tt&amp;gt; ||  || Week number in the year; numeric; first Monday is the first day of the first week || 00-53 || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Month || &amp;lt;tt&amp;gt;%B&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;F&amp;lt;/tt&amp;gt; || Full month name, text for current locale ||  || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Month || &amp;lt;tt&amp;gt;%b&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; || Abbreviated month name, text for current locale ||  || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Month || &amp;lt;tt&amp;gt;%h&amp;lt;/tt&amp;gt; ||  || Same as &amp;lt;tt&amp;gt;%b&amp;lt;/tt&amp;gt; ||  || No&lt;br /&gt;
|-&lt;br /&gt;
| Month || &amp;lt;tt&amp;gt;%m&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;m&amp;lt;/tt&amp;gt; || Month, numeric || 01-12 || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Month ||  || &amp;lt;tt&amp;gt;n&amp;lt;/tt&amp;gt; || Month, numeric, leading zero trimmed || 1-12 || No&lt;br /&gt;
|-&lt;br /&gt;
| Month ||  || &amp;lt;tt&amp;gt;t&amp;lt;/tt&amp;gt; || Month length in days, numeric || 28, 29, 30, 31 || No&lt;br /&gt;
|-&lt;br /&gt;
| Year || &amp;lt;tt&amp;gt;%C&amp;lt;/tt&amp;gt; ||  || Century, numeric || 00-99 || No&lt;br /&gt;
|-&lt;br /&gt;
| Year || &amp;lt;tt&amp;gt;%g&amp;lt;/tt&amp;gt; ||  || Like &amp;lt;tt&amp;gt;%G&amp;lt;/tt&amp;gt;, but without the century || 00-99 || No&lt;br /&gt;
|-&lt;br /&gt;
| Year || &amp;lt;tt&amp;gt;%G&amp;lt;/tt&amp;gt; ||  || ISO 8601 year with century; numeric; the four-digit year corresponding to the ISO week number; same as &amp;lt;tt&amp;gt;%y&amp;lt;/tt&amp;gt; except if the ISO week number belongs to the previous or next year, that year is used instead ||  || No&lt;br /&gt;
|-&lt;br /&gt;
| Year || &amp;lt;tt&amp;gt;%y&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;y&amp;lt;/tt&amp;gt; || Year without century, numeric || 00-99 || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Year || &amp;lt;tt&amp;gt;%Y&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;Y&amp;lt;/tt&amp;gt; || Year, numeric, including century ||  || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Year ||  || &amp;lt;tt&amp;gt;L&amp;lt;/tt&amp;gt; || Leap year flag (yes is 1) || 0, 1 || No&lt;br /&gt;
|-&lt;br /&gt;
| Timezone || &amp;lt;tt&amp;gt;%z&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;O&amp;lt;/tt&amp;gt; || Hour offset from GMT, +/-HHMM (e.g., -0400, +0230) || -1200-+1200 || Yes, but acts like &amp;lt;tt&amp;gt;%Z&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Timezone || &amp;lt;tt&amp;gt;%Z&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;T&amp;lt;/tt&amp;gt; || Time zone, name, or abbreviation; textual ||  || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Timezone ||  || &amp;lt;tt&amp;gt;I&amp;lt;/tt&amp;gt; || Daylight saving time flag (yes is 1) || 0, 1 || No&lt;br /&gt;
|-&lt;br /&gt;
| Timezone ||  || &amp;lt;tt&amp;gt;Z&amp;lt;/tt&amp;gt; || Seconds offset from GMT; west of GMT is negative, east of GMT is positive || -43200-43200 || No&lt;br /&gt;
|-&lt;br /&gt;
| Compound || &amp;lt;tt&amp;gt;%c&amp;lt;/tt&amp;gt; ||  || Standard date and time format for current locale ||  || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Compound || &amp;lt;tt&amp;gt;%D&amp;lt;/tt&amp;gt; ||  || Same as &amp;lt;tt&amp;gt;%m/%d/%y&amp;lt;/tt&amp;gt; ||  || No&lt;br /&gt;
|-&lt;br /&gt;
| Compound || &amp;lt;tt&amp;gt;%F&amp;lt;/tt&amp;gt; ||  || Same as &amp;lt;tt&amp;gt;%Y-%m-%d&amp;lt;/tt&amp;gt; ||  || No&lt;br /&gt;
|-&lt;br /&gt;
| Compound || &amp;lt;tt&amp;gt;%r&amp;lt;/tt&amp;gt; ||  || Time in AM or PM notation for current locale ||  || No&lt;br /&gt;
|-&lt;br /&gt;
| Compound || &amp;lt;tt&amp;gt;%R&amp;lt;/tt&amp;gt; ||  || Time in 24-hour notation for current locale ||  || No&lt;br /&gt;
|-&lt;br /&gt;
| Compound || &amp;lt;tt&amp;gt;%T&amp;lt;/tt&amp;gt; ||  || Time in 24-hour notation (same as &amp;lt;tt&amp;gt;%H:%M:%S&amp;lt;/tt&amp;gt;) ||  || No&lt;br /&gt;
|-&lt;br /&gt;
| Compound || &amp;lt;tt&amp;gt;%x&amp;lt;/tt&amp;gt; ||  || Standard date format for current locale(without time) ||  || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Compound || &amp;lt;tt&amp;gt;%X&amp;lt;/tt&amp;gt; ||  || Standard time format for current locale(without date) ||  || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Compound ||  || &amp;lt;tt&amp;gt;r&amp;lt;/tt&amp;gt; || RFC 822 formatted date (e.g., &amp;quot;Thu, 22 Aug 2002 16:01:07 +0200&amp;quot;) ||  || No&lt;br /&gt;
|-&lt;br /&gt;
| Other || &amp;lt;tt&amp;gt;%s&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;U&amp;lt;/tt&amp;gt; || Seconds since the epoch ||  || No&lt;br /&gt;
|-&lt;br /&gt;
| Other ||  || &amp;lt;tt&amp;gt;B&amp;lt;/tt&amp;gt; || Swatch Internet time ||  || No&lt;br /&gt;
|-&lt;br /&gt;
| Formatting || &amp;lt;tt&amp;gt;%%&amp;lt;/tt&amp;gt; ||  || Literal &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt; character ||  || Yes&lt;br /&gt;
|-&lt;br /&gt;
| Formatting || &amp;lt;tt&amp;gt;%n&amp;lt;/tt&amp;gt; ||  || Newline character ||  || No&lt;br /&gt;
|-&lt;br /&gt;
| Formatting || &amp;lt;tt&amp;gt;%t&amp;lt;/tt&amp;gt; ||  || Tab character ||  || No&lt;br /&gt;
|-&lt;br /&gt;
| colspan=&amp;quot;6&amp;quot; | [1]The range for seconds extends to 61 to account for leap seconds.&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The first argument to each function is a format string, and the second argument is an epoch timestamp. If you leave out the second argument, both functions default to the current date and time. While &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; operate over local time, they each have UTC-centric counterparts (&amp;lt;tt&amp;gt;gmdate( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;gmstrftime( )&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
The formatting characters for &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; are PHP-specific, but &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; uses the C-library &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; function. This may make &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; more understandable to someone coming to PHP from another language, but it also makes its behavior slightly different on various platforms. Windows doesn't support as many &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; formatting commands as most Unix-based systems. Also, &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; expects its formatting characters to each be preceded by a &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt; (think &amp;lt;tt&amp;gt;printf( )&amp;lt;/tt&amp;gt;), so it's easier to produce strings with lots of interpolated time and date values in them.&lt;br /&gt;
&lt;br /&gt;
For example, at 12:49 P.M. on July 15, 2002, the code to print out:&lt;br /&gt;
&lt;br /&gt;
 It's after 12 pm on July 15, 2002&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; looks like:&lt;br /&gt;
&lt;br /&gt;
 print strftime(&amp;quot;It's after %I %P on %B %d, %Y&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
With &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; it looks like:&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;It's after &amp;quot;.date('h a').' on '.date('F d, Y');&lt;br /&gt;
&lt;br /&gt;
Non-date-related characters in a format string are fine for &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt;, because it looks for the &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt; character to decide where to interpolate the appropriate time information. However, &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; doesn't have such a delimiter, so about the only extras you can tuck into the formatting string are spaces and punctuation. If you pass &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt;'s formatting string to &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 print date(&amp;quot;It's after %I %P on %B%d, %Y&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
you'd almost certainly not want what you'd get:&lt;br /&gt;
&lt;br /&gt;
 131'44 pmf31eMon, 15 Jul 2002 12:49:44 -0400 %1 %P o7 %742%15, %2002&lt;br /&gt;
&lt;br /&gt;
To generate time parts with &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; that are easy to interpolate, group all time and date parts from &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; into one string, separating the different components with a delimiter that &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; won't translate into anything and that isn't itself part of one of your substrings. Then, using &amp;lt;tt&amp;gt;explode( )&amp;lt;/tt&amp;gt; with that delimiter character, put each piece of the return value from &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; in an array, which is easily interpolated in your output string:&lt;br /&gt;
&lt;br /&gt;
 $ar = explode(':',date(&amp;quot;h a:F d, Y&amp;quot;));&lt;br /&gt;
 print &amp;quot;It's after $ar[0] on $ar[1]&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/date'' and &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/strftime''; on Unix-based systems, ''man strftime'' for your system-specific &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; options; on Windows, see ''http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strftime.2c_.wcsftime.asp'' for &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; details.&lt;br /&gt;
&lt;br /&gt;
== Finding the Difference of Two Dates ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to find the elapsed time between two dates. For example, you want to tell a user how long it's been since she last logged onto your site.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Convert both dates to epoch timestamps and subtract one from the other. Use this code to separate the difference into weeks, days, hours, minutes, and seconds:&lt;br /&gt;
&lt;br /&gt;
 // 7:32:56 pm on May 10, 1965&lt;br /&gt;
 $epoch_1 = mktime(19,32,56,5,10,1965);&lt;br /&gt;
 // 4:29:11 am on November 20, 1962&lt;br /&gt;
 $epoch_2 = mktime(4,29,11,11,20,1962);&lt;br /&gt;
 &lt;br /&gt;
 $diff_seconds  = $epoch_1 - $epoch_2;&lt;br /&gt;
 $diff_weeks    = floor($diff_seconds/604800);&lt;br /&gt;
 $diff_seconds -= $diff_weeks   * 604800;&lt;br /&gt;
 $diff_days     = floor($diff_seconds/86400);&lt;br /&gt;
 $diff_seconds -= $diff_days    * 86400;&lt;br /&gt;
 $diff_hours    = floor($diff_seconds/3600);&lt;br /&gt;
 $diff_seconds -= $diff_hours   * 3600;&lt;br /&gt;
 $diff_minutes  = floor($diff_seconds/60);&lt;br /&gt;
 $diff_seconds -= $diff_minutes * 60;&lt;br /&gt;
 &lt;br /&gt;
 print &amp;quot;The two dates have $diff_weeks weeks, $diff_days days, &amp;quot;;&lt;br /&gt;
 print &amp;quot;$diff_hours hours, $diff_minutes minutes, and $diff_seconds &amp;quot;;&lt;br /&gt;
 print &amp;quot;seconds elapsed between them.&amp;quot;;&lt;br /&gt;
 '''The two dates have 128 weeks, 6 days, 14 hours, 3 minutes,''' &lt;br /&gt;
                '''and 45 seconds elapsed between them.'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
Note that the difference isn't divided into larger chunks than weeks (i.e., months or years) because those chunks have variable length and wouldn't give an accurate count of the time difference calculated.&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
There are a few strange things going on here that you should be aware of. First of all, 1962 and 1965 precede the beginning of the epoch. Fortunately, &amp;lt;tt&amp;gt;mktime( )&amp;lt;/tt&amp;gt; fails gracefully here and produces negative epoch timestamps for each. This is okay because the absolute time value of either of these questionable timestamps isn't necessary, just the difference between the two. As long as epoch timestamps for the dates fall within the range of a signed integer, their difference is calculated correctly.&lt;br /&gt;
&lt;br /&gt;
Next, a wall clock (or calendar) reflects a slightly different amount of time change between these two dates, because they are on different sides of a DST switch. The result subtracting epoch timestamps gives is the correct amount of ''elapsed'' time, but the perceived human time change is an hour off. For example, on the Sunday morning in April when DST is activated, what's the difference between 1:30 A.M. and 4:30 A.M.? It seems like three hours, but the epoch timestamps for these two times are only 7,200 seconds apart — two hours. When a local clock springs forward an hour (or falls back an hour in October), the steady march of epoch timestamps takes no notice. Truly, only two hours have passed, although our clock manipulations make it seem like three.&lt;br /&gt;
&lt;br /&gt;
If you want to measure actual elapsed time (and you usually do), this method is fine. If you're more concerned with the difference in what a clock says at two points in time, use Julian days to compute the interval, as discussed in [[PHP Cookbook/Dates and Times#Finding the Difference of Two Dates with Julian Days|Recipe 3.7]].&lt;br /&gt;
&lt;br /&gt;
To tell a user the elapsed time since her last login, you need to find the difference between the login time and her last login time:&lt;br /&gt;
&lt;br /&gt;
 $epoch_1 = time();&lt;br /&gt;
 $r = mysql_query(&amp;quot;SELECT UNIX_TIMESTAMP(last_login) AS login &lt;br /&gt;
                  FROM user WHERE id = $id&amp;quot;) or die();&lt;br /&gt;
 $ob = mysql_fetch_object($r);&lt;br /&gt;
 $epoch_2 = $ob-&amp;gt;login;&lt;br /&gt;
 &lt;br /&gt;
 $diff_seconds  = $epoch_1 - $epoch_2;&lt;br /&gt;
 $diff_weeks    = floor($diff_seconds/604800);&lt;br /&gt;
 $diff_seconds -= $diff_weeks   * 604800;&lt;br /&gt;
 $diff_days     = floor($diff_seconds/86400);&lt;br /&gt;
 $diff_seconds -= $diff_days    * 86400;&lt;br /&gt;
 $diff_hours    = floor($diff_seconds/3600);&lt;br /&gt;
 $diff_seconds -= $diff_hours   * 3600;&lt;br /&gt;
 $diff_minutes  = floor($diff_seconds/60);&lt;br /&gt;
 $diff_seconds -= $diff_minutes * 60;&lt;br /&gt;
 &lt;br /&gt;
 print &amp;quot;You last logged in $diff_weeks weeks, $diff_days days, &amp;quot;;&lt;br /&gt;
 print &amp;quot;$diff_hours hours, $diff_minutes minutes, and $diff_seconds ago.&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Dates and Times#Finding the Difference of Two Dates with Julian Days|Recipe 3.7]] to find the difference between two dates with Julian days; [[PHP Cookbook/Dates and Times#Adding to or Subtracting from a Date|Recipe 3.11]] for adding and subtracting from a date; documentation on MySQL's &amp;lt;tt&amp;gt;UNIX_TIMESTAMP( )&amp;lt;/tt&amp;gt; function at ''http://www.mysql.com/doc/D/a/Date_and_time_functions.html''.&lt;br /&gt;
&lt;br /&gt;
== Finding the Difference of Two Dates with Julian Days ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to find the difference of two dates measured by what a clock would say, not the actual elapsed time.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;gregoriantojd( )&amp;lt;/tt&amp;gt; to get the Julian day for a set of date parts, then subtract one Julian day from the other to find the date difference. Then convert the time parts to seconds and subtract one from the other to find the time difference. If the time difference is less than 0, decrease the date difference by one and adjust the time difference to apply to the previous day. Here's the code:&lt;br /&gt;
&lt;br /&gt;
 $diff_date = gregoriantojd($date_1_mo, $date_1_dy, $date_1_yr) -&lt;br /&gt;
              gregoriantojd($date_2_mo, $date_2_dy, $date_2_yr);&lt;br /&gt;
 $diff_time = $date_1_hr * 3600 + $date_1_mn * 60 + $date_1_sc -&lt;br /&gt;
              $date_2_hr * 3600 - $date_2_mn * 60 - $date_2_sc;&lt;br /&gt;
 if ($diff_time &amp;lt; 0) {&lt;br /&gt;
    $diff_date--; &lt;br /&gt;
    $diff_time = 86400 - $diff_time;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Finding differences with Julian days lets you operate outside the range of epoch seconds and also accounts for DST differences.&lt;br /&gt;
&lt;br /&gt;
If you have the components of your two days in arrays:&lt;br /&gt;
&lt;br /&gt;
 // 7:32:56 pm on May 10, 1965&lt;br /&gt;
 list($date_1_yr, $date_1_mo, $date_1_dy, $date_1_hr, $date_1_mn, $date_1_sc)=&lt;br /&gt;
     array(1965, 5, 10, 19, 32, 56);&lt;br /&gt;
 // 4:29:11 am on November 20, 1962&lt;br /&gt;
 list($date_2_yr, $date_2_mo, $date_2_dy, $date_2_hr, $date_2_mn, $date_2_sc)=&lt;br /&gt;
     array(1962, 11, 20, 4, 29, 11);&lt;br /&gt;
 &lt;br /&gt;
 $diff_date = gregoriantojd($date_1_mo, $date_1_dy, $date_1_yr) -&lt;br /&gt;
              gregoriantojd($date_2_mo, $date_2_dy, $date_2_yr);&lt;br /&gt;
 $diff_time = $date_1_hr * 3600 + $date_1_mn * 60 + $date_1_sc -&lt;br /&gt;
              $date_2_hr * 3600 - $date_2_mn * 60 - $date_2_sc;&lt;br /&gt;
 if ($diff_time &amp;lt; 0) {&lt;br /&gt;
    $diff_date--; &lt;br /&gt;
    $diff_time = 86400 - $diff_time;&lt;br /&gt;
 }&lt;br /&gt;
 $diff_weeks = floor($diff_date/7); $diff_date -= $diff_weeks * 7;&lt;br /&gt;
 $diff_hours = floor($diff_time/3600); $diff_time -= $diff_hours * 3600;&lt;br /&gt;
 $diff_minutes = floor($diff_time/60); $diff_time -= $diff_minutes * 60;&lt;br /&gt;
 &lt;br /&gt;
 print &amp;quot;The two dates have $diff_weeks weeks, $diff_date days, &amp;quot;;&lt;br /&gt;
 print &amp;quot;$diff_hours hours, $diff_minutes minutes, and $diff_time &amp;quot;;&lt;br /&gt;
 print &amp;quot;seconds between them.&amp;quot;;&lt;br /&gt;
 '''The two dates have 128 weeks, 6 days, 15 hours, 3 minutes,'''&lt;br /&gt;
                '''and 45 seconds between them.'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
This method produces a time difference based on clock time, which is why the result shows an hour more of difference than in [[PHP Cookbook/Dates and Times#Finding the Difference of Two Dates|Recipe 3.6]]. May 10 is during DST, and November 11 is during standard time.&lt;br /&gt;
&lt;br /&gt;
The function &amp;lt;tt&amp;gt;gregoriantojd( )&amp;lt;/tt&amp;gt; is part of PHP's calendar extension, and so is available only if that extension is loaded.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Dates and Times#Finding the Difference of Two Dates|Recipe 3.6]] to find the difference between two dates in elapsed time; [[PHP Cookbook/Dates and Times#Adding to or Subtracting from a Date|Recipe 3.11]] for adding and subtracting from a date; documentation on &amp;lt;tt&amp;gt;gregoriantojd( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/gregoriantojd''; an overview of the Julian Day system is at ''http://tycho.usno.navy.mil/mjd.html''.&lt;br /&gt;
&lt;br /&gt;
== Finding the Day in a Week, Month, Year, or the Week Number in a Year ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to know the day or week of the year, the day of the week, or the day of the month. For example, you want to print a special message every Monday, or on the first of every month.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use the appropriate arguments to &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 print strftime(&amp;quot;Today is day %d of the month and %j of the year.&amp;quot;);&lt;br /&gt;
 print 'Today is day '.date('d').' of the month and '.date('z').' of the year.';&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The two functions &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; don't behave identically. Days of the year start with 0 for &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt;, but with 1 for &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt;. [[PHP Cookbook/Dates and Times#phpckbk-CHP-3-TABLE-4|Table 3-4]] contains all the day and week number format characters &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; understand.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-3-TABLE-4&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-4. Day and week number format characters'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Type !! strftime( ) !! date( ) !! Description !! Range&lt;br /&gt;
|-&lt;br /&gt;
| Day || &amp;lt;tt&amp;gt;%d&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;d&amp;lt;/tt&amp;gt; || Day of the month, numeric || 01-31&lt;br /&gt;
|-&lt;br /&gt;
| Day || &amp;lt;tt&amp;gt;%j&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;z&amp;lt;/tt&amp;gt; || Day of the year, numeric || 001-366 for &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt;; 0-365 for &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Day || &amp;lt;tt&amp;gt;%u&amp;lt;/tt&amp;gt; ||  || Day of the week, numeric (Monday is 1) || 1-7&lt;br /&gt;
|-&lt;br /&gt;
| Day || &amp;lt;tt&amp;gt;%w&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;w&amp;lt;/tt&amp;gt; || Day of the week, numeric (Sunday is 0) || 0-6&lt;br /&gt;
|-&lt;br /&gt;
| Day || &amp;lt;tt&amp;gt;%W&amp;lt;/tt&amp;gt; ||  || ISO 8601 day of the week, numeric (Monday is the first day of the week) || 0-6&lt;br /&gt;
|-&lt;br /&gt;
| Week || &amp;lt;tt&amp;gt;%U&amp;lt;/tt&amp;gt; ||  || Week number in the year; numeric; first Sunday is the first day of the first week || 00-53&lt;br /&gt;
|-&lt;br /&gt;
| Week || &amp;lt;tt&amp;gt;%V&amp;lt;/tt&amp;gt; || &lt;br /&gt;
&lt;br /&gt;
 W&lt;br /&gt;
| ISO 8601:1988 week number in the year; numeric; week 1 is the first week that has at least four days in the current year; Monday is the first day of the week || 01-53&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To print out something only on Mondays, use the &amp;lt;tt&amp;gt;w&amp;lt;/tt&amp;gt; formatting character to &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; or the &amp;lt;tt&amp;gt;%w&amp;lt;/tt&amp;gt; string with &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 if (1 == date('w')) {&lt;br /&gt;
     print &amp;quot;Welcome to the beginning of your work week.&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 if (1 == strftime('%w')) {&lt;br /&gt;
     print &amp;quot;Only 4 more days until the weekend!&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
There are different ways to calculate week numbers and days in a week, so be careful to choose the appropriate one. The ISO standard (ISO 8601), says that weeks begin on Mondays and that the days in the week are numbered 1 (Monday) through 7 (Sunday). Week 1 in a year is the first week in a year with a Thursday in that year. This means the first week in a year is the first week with a majority of its days in that year. These week numbers range from 01 to 53.&lt;br /&gt;
&lt;br /&gt;
Other week number standards range from 00 to 53, with days in a year's week 53 potentially overlapping with days in the following year's week 00.&lt;br /&gt;
&lt;br /&gt;
As long as you're consistent within your programs, you shouldn't run into any trouble, but be careful when interfacing with other PHP programs or your database. For example, MySQL's &amp;lt;tt&amp;gt;DAYOFWEEK( )&amp;lt;/tt&amp;gt; function treats Sunday as the first day of the week, but numbers the days 1 to 7, which is the ODBC standard. Its &amp;lt;tt&amp;gt;WEEKDAY( )&amp;lt;/tt&amp;gt; function, however, treats Monday as the first day of the week and numbers the days from 0 to 6. Its &amp;lt;tt&amp;gt;WEEK( )&amp;lt;/tt&amp;gt; function lets you choose whether weeks should start on Sunday or Monday, but it's incompatible with the ISO standard.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;date( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/date'' and &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/strftime''; MySQL's &amp;lt;tt&amp;gt;DAYOFWEEK( )&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WEEKDAY( )&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;WEEK( )&amp;lt;/tt&amp;gt; functions are documented at ''http://www.mysql.com/doc/D/a/Date_and_time_functions.html''.&lt;br /&gt;
&lt;br /&gt;
== Validating a Date ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to check if a date is valid. For example, you want to make sure a user hasn't provided a birthdate such as February 30, 1962.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;checkdate( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $valid = checkdate($month,$day,$year);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The function &amp;lt;tt&amp;gt;checkdate( )&amp;lt;/tt&amp;gt; returns &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt; if &amp;lt;tt&amp;gt;$month&amp;lt;/tt&amp;gt; is between 1 and 12, &amp;lt;tt&amp;gt;$year&amp;lt;/tt&amp;gt; is between 1 and 32767, and &amp;lt;tt&amp;gt;$day&amp;lt;/tt&amp;gt; is between 1 and the correct maximum number of days for &amp;lt;tt&amp;gt;$month&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;$year&amp;lt;/tt&amp;gt;. Leap years are correctly handled by &amp;lt;tt&amp;gt;checkdate( )&amp;lt;/tt&amp;gt;, and dates are rendered using the Gregorian calendar.&lt;br /&gt;
&lt;br /&gt;
Because &amp;lt;tt&amp;gt;checkdate( )&amp;lt;/tt&amp;gt; has such a broad range of valid years, you should do additional validation on user input if, for example, you're expecting a valid birthdate. ''The Guinness Book of World Records'' says the oldest person ever reached 122. To check that a birthdate indicates a user between 18 and 122 years old, use the &amp;lt;tt&amp;gt;pc_checkbirthdate( )&amp;lt;/tt&amp;gt; function shown in [[PHP Cookbook/Dates and Times#phpckbk-CHP-3-EX-1|Example 3-1]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-3-EX-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-1. pc_checkbirthdate( )'''&lt;br /&gt;
&lt;br /&gt;
 function pc_checkbirthdate($month,$day,$year) {&lt;br /&gt;
     $min_age = 18;&lt;br /&gt;
     $max_age = 122;&lt;br /&gt;
 &lt;br /&gt;
     if (! checkdate($month,$day,$year)) {&lt;br /&gt;
         return false;&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     list($this_year,$this_month,$this_day) = explode(',',date('Y,m,d'));&lt;br /&gt;
 &lt;br /&gt;
     $min_year = $this_year - $max_age;&lt;br /&gt;
     $max_year = $this_year - $min_age;&lt;br /&gt;
 &lt;br /&gt;
     print &amp;quot;$min_year,$max_year,$month,$day,$year\n&amp;quot;;&lt;br /&gt;
     &lt;br /&gt;
     if (($year &amp;gt; $min_year) &amp;amp;&amp;amp; ($year &amp;lt; $max_year)) {&lt;br /&gt;
         return true;&lt;br /&gt;
     } elseif (($year == $max_year) &amp;amp;&amp;amp; &lt;br /&gt;
               (($month &amp;lt; $this_month) ||&lt;br /&gt;
                (($month == $this_month) &amp;amp;&amp;amp; ($day &amp;lt;= $this_day)))) {&lt;br /&gt;
         return true;&lt;br /&gt;
     } elseif (($year == $min_year) &amp;amp;&amp;amp;&lt;br /&gt;
               (($month &amp;gt; $this_month) ||&lt;br /&gt;
                (($month == $this_month &amp;amp;&amp;amp; ($day &amp;gt; $this_day))))) {&lt;br /&gt;
         return true;&lt;br /&gt;
     } else {&lt;br /&gt;
         return false;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is some sample usage:&lt;br /&gt;
&lt;br /&gt;
 // check December 3, 1974&lt;br /&gt;
 if (pc_checkbirthdate(12,3,1974)) {&lt;br /&gt;
     print &amp;quot;You may use this web site.&amp;quot;;&lt;br /&gt;
 } else {&lt;br /&gt;
     print &amp;quot;You are too young to proceed.&amp;quot;;&lt;br /&gt;
     exit( );&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This function first uses &amp;lt;tt&amp;gt;checkdate( )&amp;lt;/tt&amp;gt; to make sure that &amp;lt;tt&amp;gt;$month&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;$day&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;$year&amp;lt;/tt&amp;gt; represent a valid date. Various comparisons then make sure that the supplied date is in the range set by &amp;lt;tt&amp;gt;$min_age&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;$max_age&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;tt&amp;gt;$year&amp;lt;/tt&amp;gt; is noninclusively between &amp;lt;tt&amp;gt;$min_year&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;$max_year&amp;lt;/tt&amp;gt;, the date is definitely within the range, and the function returns &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt;. If not, some additional checks are required. If &amp;lt;tt&amp;gt;$year&amp;lt;/tt&amp;gt; equals &amp;lt;tt&amp;gt;$max_year&amp;lt;/tt&amp;gt; (e.g., in 2002, &amp;lt;tt&amp;gt;$year&amp;lt;/tt&amp;gt; is 1984), &amp;lt;tt&amp;gt;$month&amp;lt;/tt&amp;gt; must be before the current month. If &amp;lt;tt&amp;gt;$month&amp;lt;/tt&amp;gt; equals the current month, &amp;lt;tt&amp;gt;$day&amp;lt;/tt&amp;gt; must be before or equal to the current day. If &amp;lt;tt&amp;gt;$year&amp;lt;/tt&amp;gt; equals &amp;lt;tt&amp;gt;$min_year&amp;lt;/tt&amp;gt; (e.g., in 2002, &amp;lt;tt&amp;gt;$year&amp;lt;/tt&amp;gt; is 1880), &amp;lt;tt&amp;gt;$month&amp;lt;/tt&amp;gt; must be after the current month. If &amp;lt;tt&amp;gt;$month&amp;lt;/tt&amp;gt; equals the current month, &amp;lt;tt&amp;gt;$day&amp;lt;/tt&amp;gt; must be after the current day. If none of these conditions are met, the supplied date is outside the appropriate range, and the function returns &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The function returns &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt; if the supplied date is exactly &amp;lt;tt&amp;gt;$min_age&amp;lt;/tt&amp;gt; years before the current date, but &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; if the supplied date is exactly &amp;lt;tt&amp;gt;$max_age&amp;lt;/tt&amp;gt; years after the current date. That is, it would let you through on your 18th birthday, but not on your 123rd.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;checkdate( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/checkdate''; information about ''The Guinness Book'''s oldest person is at ''http://www.guinnessworldrecords.com'' (navigate to &amp;quot;The Human Body,&amp;quot; &amp;quot;Age and Youth,&amp;quot; and then &amp;quot;Oldest Woman Ever&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
== Parsing Dates and Times from Strings ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to get a date or time in a string into a format you can use in calculations. For example, you want to convert date expressions such as &amp;quot;last Thursday&amp;quot; into an epoch timestamp.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
The simplest way to parse a date or time string is with &amp;lt;tt&amp;gt;strtotime( )&amp;lt;/tt&amp;gt; , which turns a variety of human-readable date and time strings into epoch timestamps:&lt;br /&gt;
&lt;br /&gt;
 $a = strtotime('march 10'); // defaults to the current year&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The grammar &amp;lt;tt&amp;gt;strtotime( )&amp;lt;/tt&amp;gt; uses is both complicated and comprehensive so the best way to get comfortable with it is to try out lots of different time expressions. If you're curious about its nuts and bolts, check out ''ext/standard/parsedate.y'' in the PHP source distribution.&lt;br /&gt;
&lt;br /&gt;
The function &amp;lt;tt&amp;gt;strtotime( )&amp;lt;/tt&amp;gt; understands words about the current time:&lt;br /&gt;
&lt;br /&gt;
 $a = strtotime('now');&lt;br /&gt;
 print strftime('%c',$a);&lt;br /&gt;
 $a = strtotime('today');&lt;br /&gt;
 print strftime('%c',$a);&lt;br /&gt;
 '''Mon Aug 12 20:35:10 2002'''&lt;br /&gt;
                '''Mon Aug 12 20:35:10 2002'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
It understands different ways to identify a time and date:&lt;br /&gt;
&lt;br /&gt;
 $a = strtotime('5/12/1994');&lt;br /&gt;
 print strftime('%c',$a);&lt;br /&gt;
 $a = strtotime('12 may 1994');&lt;br /&gt;
 print strftime('%c',$a);&lt;br /&gt;
 '''Thu May 12 00:00:00 1994'''&lt;br /&gt;
                '''Thu May 12 00:00:00 1994'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
It understands relative times and dates:&lt;br /&gt;
&lt;br /&gt;
 $a = strtotime('last thursday');   // On August 12, 2002&lt;br /&gt;
 print strftime('%c',$a);&lt;br /&gt;
 $a = strtotime('2001-07-12 2pm + 1 month');&lt;br /&gt;
 print strftime('%c',$a);&lt;br /&gt;
 '''Thu Aug  8 00:00:00 2002'''&lt;br /&gt;
                '''Mon Aug 12 14:00:00 2002'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
It understands time zones. When the following is run from a computer in EDT, it prints out the same time:&lt;br /&gt;
&lt;br /&gt;
 $a = strtotime('2002-07-12 2pm edt + 1 month');&lt;br /&gt;
 print strftime('%c',$a);&lt;br /&gt;
 '''Mon Aug 12 14:00:00 2002'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
However, when the following is run from a computer in EDT, it prints out the time in EDT when it is 2 P.M. in MDT (two hours before EDT):&lt;br /&gt;
&lt;br /&gt;
 $a = strtotime('2002-07-12 2pm mdt + 1 month');&lt;br /&gt;
 print strftime('%c',$a);&lt;br /&gt;
 '''Mon Aug 12 16:00:00 2002'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
If the date and time you want to parse out of a string are in a format you know in advance, instead of calling &amp;lt;tt&amp;gt;strtotime( )&amp;lt;/tt&amp;gt;, you can build a regular expression that grabs the different date and time parts you need. For example, here's how to parse &amp;quot;YYYY-MM-DD HH:MM:SS&amp;quot; dates, such as a MySQL &amp;lt;tt&amp;gt;DATETIME&amp;lt;/tt&amp;gt; field:&lt;br /&gt;
&lt;br /&gt;
 $date = '1974-12-03 05:12:56';&lt;br /&gt;
 preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/',$date,$date_parts);&lt;br /&gt;
&lt;br /&gt;
This puts the year, month, day, hour, minute, and second into &amp;lt;tt&amp;gt;$date_parts[1]&amp;lt;/tt&amp;gt; through &amp;lt;tt&amp;gt;$date_parts[6]&amp;lt;/tt&amp;gt;. (&amp;lt;tt&amp;gt;preg_match( )&amp;lt;/tt&amp;gt; puts the entire matched expression into &amp;lt;tt&amp;gt;$date_parts[0]&amp;lt;/tt&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
You can use regular expressions to pull the date and time out of a larger string that might also contain other information (from user input, or a file you're reading), but if you're sure about the position of the date in the string you're parsing, you can use &amp;lt;tt&amp;gt;substr( )&amp;lt;/tt&amp;gt; to make it even faster:&lt;br /&gt;
&lt;br /&gt;
 $date_parts[0] = substr($date,0,4);&lt;br /&gt;
 $date_parts[1] = substr($date,5,2);&lt;br /&gt;
 $date_parts[2] = substr($date,8,2);&lt;br /&gt;
 $date_parts[3] = substr($date,11,2);&lt;br /&gt;
 $date_parts[4] = substr($date,14,2);&lt;br /&gt;
 $date_parts[5] = substr($date,17,2);&lt;br /&gt;
&lt;br /&gt;
You can also use &amp;lt;tt&amp;gt;split( )&amp;lt;/tt&amp;gt;;&lt;br /&gt;
&lt;br /&gt;
 $ar = split('[- :]',$date);&lt;br /&gt;
 print_r($ar);&lt;br /&gt;
 '''Array'''&lt;br /&gt;
                '''('''&lt;br /&gt;
                '''    [0] =&amp;gt; 1974'''&lt;br /&gt;
                '''    [1] =&amp;gt; 12'''&lt;br /&gt;
                '''    [2] =&amp;gt; 03'''&lt;br /&gt;
                '''    [3] =&amp;gt; 05'''&lt;br /&gt;
                '''    [4] =&amp;gt; 12'''&lt;br /&gt;
                '''    [5] =&amp;gt; 56'''&lt;br /&gt;
                ''')'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
Be careful: PHP converts between numbers and strings without any prompting, but numbers beginning with a 0 are considered to be in octal (base 8). So, 03 and 05 are 3 and 5; but, 08 and 09 are ''not'' 8 and 9.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;preg_match( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;strtotime( )&amp;lt;/tt&amp;gt; are equally efficient in parsing a date format such as &amp;quot;YYYY-MM-DD HH:MM:SS&amp;quot;, but &amp;lt;tt&amp;gt;ereg( )&amp;lt;/tt&amp;gt; is about four times slower than either. If you need the individual parts of the date string, &amp;lt;tt&amp;gt;preg_match( )&amp;lt;/tt&amp;gt; is more convenient, but &amp;lt;tt&amp;gt;strtotime( )&amp;lt;/tt&amp;gt; is obviously much more flexible.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;strtotime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/strtotime''; the grammar for &amp;lt;tt&amp;gt;strtotime( )&amp;lt;/tt&amp;gt; is available at ''http://cvs.php.net/cvs.php/php4/ext/standard/parsedate.y''.&lt;br /&gt;
&lt;br /&gt;
== Adding to or Subtracting from a Date ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to add or subtract an interval from a date.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Depending on how your date and interval are represented, use &amp;lt;tt&amp;gt;strtotime( )&amp;lt;/tt&amp;gt; or some simple arithmetic.&lt;br /&gt;
&lt;br /&gt;
If you have your date and interval in appropriate formats, the easiest thing to do is use &amp;lt;tt&amp;gt;strtotime( )&amp;lt;/tt&amp;gt; :&lt;br /&gt;
&lt;br /&gt;
 $birthday = 'March 10, 1975';&lt;br /&gt;
 $whoopee_made = strtotime(&amp;quot;$birthday - 9 months ago&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
If your date in an epoch timestamp and you can express your interval in seconds, subtract the interval from the timestamp:&lt;br /&gt;
&lt;br /&gt;
 $birthday = 163727100;&lt;br /&gt;
 $gestation = 36 * 7 * 86400; // 36 weeks&lt;br /&gt;
 $whoopee_made = $birthday - $gestation;&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Using &amp;lt;tt&amp;gt;strtotime( )&amp;lt;/tt&amp;gt; is good for intervals that are of varying lengths, like months. If you can't use &amp;lt;tt&amp;gt;strtotime( )&amp;lt;/tt&amp;gt;, you can convert your date to an epoch timestamp and add or subtract the appropriate interval in seconds. This is mostly useful for intervals of a fixed time, such as days or weeks:&lt;br /&gt;
&lt;br /&gt;
 $now = time( );&lt;br /&gt;
 $next_week = $now + 7 * 86400;&lt;br /&gt;
&lt;br /&gt;
Using this method, however, you can run into problems if the endpoints of your interval are on different sides of a DST switch. In this case, one of your fixed length days isn't 86,400 seconds long; it's either 82,800 or 90,000 seconds long, depending on the season. If you use UTC exclusively in your application, you don't have to worry about this. But if you have to use local time, you can count days without worrying about this hiccup with Julian days. You can convert between epoch timestamps and Julian days with &amp;lt;tt&amp;gt;unixtojd( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;jdtounix( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $now = time( );&lt;br /&gt;
 $today = unixtojd($now);&lt;br /&gt;
 $next_week = jdtounix($today + 7);&lt;br /&gt;
 // don't forget to add back hours, minutes, and seconds&lt;br /&gt;
 $next_week += 3600 * date('H',$now) + 60 * date('i',$now) + date('s',$now);&lt;br /&gt;
&lt;br /&gt;
The functions &amp;lt;tt&amp;gt;unixtojd( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;jdtounix( )&amp;lt;/tt&amp;gt; are part of PHP's calendar extension, so they are only available if that extension is loaded.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Dates and Times#Finding the Difference of Two Dates|Recipe 3.6]] for finding the difference between two dates in elapsed time; [[PHP Cookbook/Dates and Times#Finding the Difference of Two Dates with Julian Days|Recipe 3.7]] for finding the difference between two dates in Julian days; documentation on &amp;lt;tt&amp;gt;strtotime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/strtotime'', &amp;lt;tt&amp;gt;unixtojd( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/unixtojd'', and &amp;lt;tt&amp;gt;jdtounix( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/jdtounix''.&lt;br /&gt;
&lt;br /&gt;
== Calculating Time with Time Zones ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to calculate times in different time zones. For example, you want to give users information adjusted to their local time, not the local time of your server.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
For simple calculations, you can explicitly add or subtract the offsets between two time zones:&lt;br /&gt;
&lt;br /&gt;
 // If local time is EST&lt;br /&gt;
 $time_parts = localtime();&lt;br /&gt;
 // California (PST) is three hours earlier&lt;br /&gt;
 $california_time_parts = localtime(time() - 3 * 3600);&lt;br /&gt;
&lt;br /&gt;
On Unix-based systems, if you don't know the offsets between time zones, just set the &amp;lt;tt&amp;gt;TZ&amp;lt;/tt&amp;gt; environment variable to your target time zone:&lt;br /&gt;
&lt;br /&gt;
 putenv('TZ=PST8PDT');&lt;br /&gt;
 $california_time_parts = localtime();&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Before we sink too deeply into the ins and outs of time zones, we want to pass along the disclaimer that the U.S. Naval Observatory offers at ''http://tycho.usno.navy.mil/tzones.html''. Namely, official worldwide time-zone information is somewhat fragile &amp;quot;because nations are sovereign powers that can and do change their timekeeping systems as they see fit.&amp;quot; So, remembering that we are at the mercy of the vagaries of international relations, here are some ways to cope with Earth's many time zones.&lt;br /&gt;
&lt;br /&gt;
For a relatively simple treatment of offsets between time zones, use an array in your program that has the various time zones' offsets from UTC. Once you determine what time zone your user is in, just add that offset to the appropriate UTC time and the functions that print out UTC time (e.g., &amp;lt;tt&amp;gt;gmdate( )&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;gmstrftime( )&amp;lt;/tt&amp;gt;) can print out the correct adjusted time.&lt;br /&gt;
&lt;br /&gt;
 // Find the current time &lt;br /&gt;
 $now = time();&lt;br /&gt;
 &lt;br /&gt;
 // California is 8 hours behind UTC&lt;br /&gt;
 $now += $pc_timezones['PST'];&lt;br /&gt;
 &lt;br /&gt;
 // Use gmdate() or gmstrftime() to print California-appropriate time&lt;br /&gt;
 print gmstrftime('%c',$now);&lt;br /&gt;
&lt;br /&gt;
The previous code uses this &amp;lt;tt&amp;gt;$pc_timezones&amp;lt;/tt&amp;gt; array, which contains offsets from UTC:&lt;br /&gt;
&lt;br /&gt;
 // From Perl's Time::Timezone&lt;br /&gt;
 $pc_timezones = array(&lt;br /&gt;
   'GMT'  =&amp;gt;   0,           // Greenwich Mean&lt;br /&gt;
   'UTC'  =&amp;gt;   0,           // Universal (Coordinated)&lt;br /&gt;
   'WET'  =&amp;gt;   0,           // Western European&lt;br /&gt;
   'WAT'  =&amp;gt;  -1*3600,      // West Africa&lt;br /&gt;
   'AT'   =&amp;gt;  -2*3600,      // Azores&lt;br /&gt;
   'NFT'  =&amp;gt;  -3*3600-1800, // Newfoundland&lt;br /&gt;
   'AST'  =&amp;gt;  -4*3600,      // Atlantic Standard&lt;br /&gt;
   'EST'  =&amp;gt;  -5*3600,      // Eastern Standard&lt;br /&gt;
   'CST'  =&amp;gt;  -6*3600,      // Central Standard&lt;br /&gt;
   'MST'  =&amp;gt;  -7*3600,      // Mountain Standard&lt;br /&gt;
   'PST'  =&amp;gt;  -8*3600,      // Pacific Standard&lt;br /&gt;
   'YST'  =&amp;gt;  -9*3600,      // Yukon Standard&lt;br /&gt;
   'HST'  =&amp;gt; -10*3600,      // Hawaii Standard&lt;br /&gt;
   'CAT'  =&amp;gt; -10*3600,      // Central Alaska&lt;br /&gt;
   'AHST' =&amp;gt; -10*3600,      // Alaska-Hawaii Standard&lt;br /&gt;
   'NT'   =&amp;gt; -11*3600,      // Nome&lt;br /&gt;
   'IDLW' =&amp;gt; -12*3600,      // International Date Line West&lt;br /&gt;
   'CET'  =&amp;gt;  +1*3600,      // Central European&lt;br /&gt;
   'MET'  =&amp;gt;  +1*3600,      // Middle European&lt;br /&gt;
   'MEWT' =&amp;gt;  +1*3600,      // Middle European Winter&lt;br /&gt;
   'SWT'  =&amp;gt;  +1*3600,      // Swedish Winter&lt;br /&gt;
   'FWT'  =&amp;gt;  +1*3600,      // French Winter&lt;br /&gt;
   'EET'  =&amp;gt;  +2*3600,      // Eastern Europe, USSR Zone 1&lt;br /&gt;
   'BT'   =&amp;gt;  +3*3600,      // Baghdad, USSR Zone 2&lt;br /&gt;
   'IT'   =&amp;gt;  +3*3600+1800, // Iran&lt;br /&gt;
   'ZP4'  =&amp;gt;  +4*3600,      // USSR Zone 3&lt;br /&gt;
   'ZP5'  =&amp;gt;  +5*3600,      // USSR Zone 4&lt;br /&gt;
   'IST'  =&amp;gt;  +5*3600+1800, // Indian Standard&lt;br /&gt;
   'ZP6'  =&amp;gt;  +6*3600,      // USSR Zone 5&lt;br /&gt;
   'SST'  =&amp;gt;  +7*3600,      // South Sumatra, USSR Zone 6&lt;br /&gt;
   'WAST' =&amp;gt;  +7*3600,      // West Australian Standard&lt;br /&gt;
   'JT'   =&amp;gt;  +7*3600+1800, // Java &lt;br /&gt;
   'CCT'  =&amp;gt;  +8*3600,      // China Coast, USSR Zone 7&lt;br /&gt;
   'JST'  =&amp;gt;  +9*3600,      // Japan Standard, USSR Zone 8&lt;br /&gt;
   'CAST' =&amp;gt;  +9*3600+1800, // Central Australian Standard&lt;br /&gt;
   'EAST' =&amp;gt; +10*3600,      // Eastern Australian Standard&lt;br /&gt;
   'GST'  =&amp;gt; +10*3600,      // Guam Standard, USSR Zone 9&lt;br /&gt;
   'NZT'  =&amp;gt; +12*3600,      // New Zealand&lt;br /&gt;
   'NZST' =&amp;gt; +12*3600,      // New Zealand Standard&lt;br /&gt;
   'IDLE' =&amp;gt; +12*3600       // International Date Line East&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
On Unix systems, you can use the ''zoneinfo'' library to do the conversions. This makes your code more compact and also transparently handles DST, as discussed in [[PHP Cookbook/Dates and Times#Accounting for Daylight Saving Time|Recipe 3.13]].&lt;br /&gt;
&lt;br /&gt;
To take advantage of ''zoneinfo'' in PHP, do all your internal date math with epoch timestamps. Generate them from time parts with the &amp;lt;tt&amp;gt;pc_mktime( )&amp;lt;/tt&amp;gt; function shown in [[PHP Cookbook/Dates and Times#phpckbk-CHP-3-EX-2|Example 3-2]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-3-EX-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-2. pc_mktime( )'''&lt;br /&gt;
&lt;br /&gt;
 function pc_mktime($tz,$hr,$min,$sec,$mon,$day,$yr) {&lt;br /&gt;
     putenv(&amp;quot;TZ=$tz&amp;quot;);&lt;br /&gt;
     $a = mktime($hr,$min,$sec,$mon,$day,$yr);&lt;br /&gt;
     putenv('TZ=EST5EDT');   // change EST5EDT to your server's time zone!&lt;br /&gt;
     return $a;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling &amp;lt;tt&amp;gt;putenv( )&amp;lt;/tt&amp;gt; before &amp;lt;tt&amp;gt;mktime( )&amp;lt;/tt&amp;gt; fools the system functions &amp;lt;tt&amp;gt;mktime( )&amp;lt;/tt&amp;gt; uses into thinking they're in a different time zone. After the call to &amp;lt;tt&amp;gt;mktime( )&amp;lt;/tt&amp;gt;, the correct time zone has to be restored. On the East Coast of the United States, that's &amp;lt;tt&amp;gt;EST5EDT&amp;lt;/tt&amp;gt;. Change this to the appropriate value for your computer's location (see [[PHP Cookbook/Dates and Times#phpckbk-CHP-3-TABLE-5|Table 3-5]]).&lt;br /&gt;
&lt;br /&gt;
Time parts are turned into epoch timestamps by &amp;lt;tt&amp;gt;pc_mktime( )&amp;lt;/tt&amp;gt;. Its counterpart, to turn epoch timestamps into formatted time strings and time parts, is &amp;lt;tt&amp;gt;pc_strftime( )&amp;lt;/tt&amp;gt;, shown in [[PHP Cookbook/Dates and Times#phpckbk-CHP-3-EX-3|Example 3-3]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-3-EX-3&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-3. pc_strftime( )'''&lt;br /&gt;
&lt;br /&gt;
 function pc_strftime($tz,$format,$timestamp) {&lt;br /&gt;
     putenv(&amp;quot;TZ=$tz&amp;quot;);&lt;br /&gt;
     $a = strftime($format,$timestamp);&lt;br /&gt;
     putenv('TZ=EST5EDT');   // change EST5EDT to your server's time zone!&lt;br /&gt;
     return $a;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example uses the same system-function-fooling &amp;lt;tt&amp;gt;pc_mktime( )&amp;lt;/tt&amp;gt; does to get the right results from &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The great thing about these functions is that you don't have to worry about the offsets from UTC of different time zones, whether DST is in effect, or any other irregularities of time-zone differences. You just set the appropriate zone and let your system's libraries do the rest.&lt;br /&gt;
&lt;br /&gt;
Note that the value of the &amp;lt;tt&amp;gt;$tz&amp;lt;/tt&amp;gt; variable in both these functions should not be a time-zone name but a ''zoneinfo'' zone. ''zoneinfo'' zones are more specific than time zones, because they correspond to particular places. [[PHP Cookbook/Dates and Times#phpckbk-CHP-3-TABLE-5|Table 3-5]] contains mappings for appropriate ''zoneinfo'' zones for some UTC offsets. The last column indicates whether the zone observes DST.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-3-TABLE-5&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-5. zoneinfo zones'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! UTC offset (hours) !! UTC offset (seconds) !! zoneinfo zone !! DST?&lt;br /&gt;
|-&lt;br /&gt;
| -12 || -43200 || &amp;lt;tt&amp;gt;Etc/GMT+12&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| -11 || -39600 || &amp;lt;tt&amp;gt;Pacific/Midway&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| -10 || -36000 || &amp;lt;tt&amp;gt;US/Aleutian&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| -10 || -36000 || &amp;lt;tt&amp;gt;Pacific/Honolulu&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| -9 || -32400 || &amp;lt;tt&amp;gt;America/Anchorage&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| -9 || -32400 || &amp;lt;tt&amp;gt;Etc/GMT+9&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| -8 || -28800 || &amp;lt;tt&amp;gt;PST8PDT&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| -8 || -28800 || &amp;lt;tt&amp;gt;America/Dawson_Creek&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| -7 || -25200 || &amp;lt;tt&amp;gt;MST7MDT&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| -7 || -25200 || &amp;lt;tt&amp;gt;MST&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| -6 || -21600 || &amp;lt;tt&amp;gt;CST6CDT&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| -6 || -21600 || &amp;lt;tt&amp;gt;Canada/Saskatchewan&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| -5 || -18000 || &amp;lt;tt&amp;gt;EST5EDT&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| -5 || -18000 || &amp;lt;tt&amp;gt;EST&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| -4 || -14400 || &amp;lt;tt&amp;gt;America/Halifax&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| -4 || -14400 || &amp;lt;tt&amp;gt;America/Puerto_Rico&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| -3.5 || -12600 || &amp;lt;tt&amp;gt;America/St_Johns&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| -3 || -10800 || &amp;lt;tt&amp;gt;America/Buenos_Aires&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 0 || 0 || &amp;lt;tt&amp;gt;Europe/London&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| 0 || 0 || &amp;lt;tt&amp;gt;GMT&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 1 || 3600 || &amp;lt;tt&amp;gt;CET&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| 1 || 3600 || &amp;lt;tt&amp;gt;GMT-1&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 2 || 7200 || &amp;lt;tt&amp;gt;EET&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 2 || 7200 || &amp;lt;tt&amp;gt;GMT-2&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 3 || 10800 || &amp;lt;tt&amp;gt;Asia/Baghdad&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| 3 || 10800 || &amp;lt;tt&amp;gt;GMT-3&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 3.5 || 12600 || &amp;lt;tt&amp;gt;Asia/Tehran&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| 4 || 14400 || &amp;lt;tt&amp;gt;Asia/Dubai&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 4 || 14400 || &amp;lt;tt&amp;gt;Asia/Baku&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| 4.5 || 16200 || &amp;lt;tt&amp;gt;Asia/Kabul&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 5 || 18000 || &amp;lt;tt&amp;gt;Asia/Tashkent&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 5.5 || 19800 || &amp;lt;tt&amp;gt;Asia/Calcutta&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 5.75 || 20700 || &amp;lt;tt&amp;gt;Asia/Katmandu&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 6 || 21600 || &amp;lt;tt&amp;gt;Asia/Novosibirsk&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| 6 || 21600 || &amp;lt;tt&amp;gt;Etc/GMT-6&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 6.5 || 23400 || &amp;lt;tt&amp;gt;Asia/Rangoon&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 7 || 25200 || &amp;lt;tt&amp;gt;Asia/Jakarta&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 8 || 28800 || &amp;lt;tt&amp;gt;Hongkong&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 9 || 32400 || &amp;lt;tt&amp;gt;Japan&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 9.5 || 34200 || &amp;lt;tt&amp;gt;Australia/Darwin&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 10 || 36000 || &amp;lt;tt&amp;gt;Australia/Sydney&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|-&lt;br /&gt;
| 10 || 36000 || &amp;lt;tt&amp;gt;Pacific/Guam&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 12 || 43200 || &amp;lt;tt&amp;gt;Etc/GMT-13&amp;lt;/tt&amp;gt; || No&lt;br /&gt;
|-&lt;br /&gt;
| 12 || 43200 || &amp;lt;tt&amp;gt;Pacific/Auckland&amp;lt;/tt&amp;gt; || Yes&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Countries around the world don't begin and end DST observance on the same days or at the same times. To calculate time appropriately for an international DST-observing location, pick a ''zoneinfo'' zone that matches your desired location as specifically as possible.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Dates and Times#Accounting for Daylight Saving Time|Recipe 3.13]] for dealing with DST; documentation on &amp;lt;tt&amp;gt;putenv( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/putenv'', &amp;lt;tt&amp;gt;localtime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/localtime'', &amp;lt;tt&amp;gt;gmdate( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/gmdate'', and &amp;lt;tt&amp;gt;gmstrftime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/gmstrftime''; ''zoneinfo'' zone names and longitude and latitude coordinates for hundreds of places around the world are available at ''ftp://elsie.nci.nih.gov/pub/tzdata2002c.tar.gz''; many links to historical and technical information about time zones can be found at ''http://www.twinsun.com/tz/tz-link.htm''.&lt;br /&gt;
&lt;br /&gt;
== Accounting for Daylight Saving Time ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to make sure your time calculations properly consider DST.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
The ''zoneinfo'' library calculates the effects of DST properly. If you are using a Unix-based system, take advantage of ''zoneinfo'' with &amp;lt;tt&amp;gt;putenv( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 putenv('TZ=MST7MDT');&lt;br /&gt;
 print strftime('%c');&lt;br /&gt;
&lt;br /&gt;
If you can't use ''zoneinfo'', you can modify hardcoded time-zone offsets based on whether the local time zone is currently observing DST. Use &amp;lt;tt&amp;gt;localtime( )&amp;lt;/tt&amp;gt; to determine the current DST observance status:&lt;br /&gt;
&lt;br /&gt;
 // Find the current UTC time &lt;br /&gt;
 $now = time();&lt;br /&gt;
 &lt;br /&gt;
 // California is 8 hours behind UTC&lt;br /&gt;
 $now -= 8 * 3600;&lt;br /&gt;
 &lt;br /&gt;
 // Is it DST? &lt;br /&gt;
 $ar = localtime($now,true);&lt;br /&gt;
 if ($ar['tm_isdst']) { $now += 3600; }&lt;br /&gt;
 &lt;br /&gt;
 // Use gmdate() or gmstrftime() to print California-appropriate time&lt;br /&gt;
 print gmstrftime('%c',$now);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Altering an epoch timestamp by the amount of a time zone's offset from UTC and then using &amp;lt;tt&amp;gt;gmdate( )&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;gmstrftime( )&amp;lt;/tt&amp;gt; to print out time zone-appropriate functions is flexible — it works from any time zone — but the DST calculations are slightly inaccurate. For the brief intervals when the server's DST status is different from the target time zone's, the results are incorrect. For example, at 3:30 A.M. EDT on the first Sunday in April (after the switch to DST), it's still before the switch (11:30 P.M.) in the Pacific time zone. A server in Eastern time using this method calculates California time to be seven hours behind UTC, whereas it's actually eight hours. At 6:00 A.M. EDT (3:00 A.M. PDT), both Pacific and Eastern time are observing DST, and the calculation is correct again (putting California at seven hours behind UTC).&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Dates and Times#Calculating Time with Time Zones|Recipe 3.12]] for dealing with time zones; documentation on &amp;lt;tt&amp;gt;putenv( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/putenv'', &amp;lt;tt&amp;gt;localtime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/localtime'', &amp;lt;tt&amp;gt;gmdate( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/gmdate'', and &amp;lt;tt&amp;gt;gmstrftime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/gmstrftime''; a detailed presentation on DST is at ''http://webexhibits.org/daylightsaving/''.&lt;br /&gt;
&lt;br /&gt;
== Generating a High-Precision Time ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to measure time with finer than one-second resolution, for example to generate a unique ID.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;microtime( )&amp;lt;/tt&amp;gt; :&lt;br /&gt;
&lt;br /&gt;
 list($microseconds,$seconds) = explode(' ',microtime());&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The function &amp;lt;tt&amp;gt;microtime( )&amp;lt;/tt&amp;gt; returns a string that contains the microseconds part of elapsed time since the epoch, a space, and seconds since the epoch. For example, a return value of &amp;lt;tt&amp;gt;0.41644100 1026683258&amp;lt;/tt&amp;gt; means that 1026683258.41644100 seconds have elapsed since the epoch. A string is returned instead of a double because the double doesn't have enough capacity to hold the entire value with microsecond precision.&lt;br /&gt;
&lt;br /&gt;
Time including microseconds is useful for generating unique IDs. When combined with the current process ID, it guarantees a unique ID, as long as a process doesn't generate more than one ID per microsecond:&lt;br /&gt;
&lt;br /&gt;
 list($microseconds,$seconds) = explode(' ',microtime());&lt;br /&gt;
 $id = $seconds.$microseconds.getmypid();&lt;br /&gt;
&lt;br /&gt;
However, this method is not as foolproof on multithreaded systems, where there is a nonzero (but very tiny) chance that two threads of the same process could call &amp;lt;tt&amp;gt;microtime( )&amp;lt;/tt&amp;gt; simultaneously.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;microtime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/microtime''.&lt;br /&gt;
&lt;br /&gt;
== Generating Time Ranges ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to know all the days in a week or a month. For example, you want to print out a list of appointments for a week.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Identify your start date using &amp;lt;tt&amp;gt;time( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt;. If your interval has a fixed length, you can loop through that many days. If not, you need to test each subsequent day for membership in your desired range.&lt;br /&gt;
&lt;br /&gt;
For example, a week has seven days, so you can use a fixed loop to generate all the days in the current week:&lt;br /&gt;
&lt;br /&gt;
 // generate a time range for this week&lt;br /&gt;
 $now = time();&lt;br /&gt;
 &lt;br /&gt;
 // If it's before 3 AM, increment $now, so we don't get caught by DST&lt;br /&gt;
 // when moving back to the beginning of the week&lt;br /&gt;
 if (3 &amp;lt; strftime('%H', $now)) { $now += 7200; }&lt;br /&gt;
 &lt;br /&gt;
 // What day of the week is today?&lt;br /&gt;
 $today = strftime('%w', $now);&lt;br /&gt;
 &lt;br /&gt;
 // How many days ago was the start of the week?&lt;br /&gt;
 $start_day = $now - (86400 * $today);&lt;br /&gt;
 &lt;br /&gt;
 // Print out each day of the week&lt;br /&gt;
 for ($i = 0; $i &amp;lt; 7; $i++) {&lt;br /&gt;
   print strftime('%c',$start_day + 86400 * $i);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
A particular month or year could have a variable number of days, so you need to compute the end of the time range based on the specifics of that month or year. To loop through every day in a month, find the epoch timestamps for the first day of the month and the first day of the next month. The loop variable, &amp;lt;tt&amp;gt;$day&amp;lt;/tt&amp;gt; is incremented a day at a time (86400 seconds) until it's no longer less than the epoch timestamp at the beginning of the next month:&lt;br /&gt;
&lt;br /&gt;
 // Generate a time range for this month&lt;br /&gt;
 $now = time();&lt;br /&gt;
 &lt;br /&gt;
 // If it's before 3 AM, increment $now, so we don't get caught by DST&lt;br /&gt;
 // when moving back to the beginning of the month&lt;br /&gt;
 if (3 &amp;lt; strftime('%H', $now)) { $now += 7200; }&lt;br /&gt;
 &lt;br /&gt;
 // What month is this?&lt;br /&gt;
 $this_month = strftime('%m',$now);&lt;br /&gt;
 &lt;br /&gt;
 // Epoch timestamp for midnight on the first day of this month&lt;br /&gt;
 $day = mktime(0,0,0,$this_month,1);&lt;br /&gt;
 // Epoch timestamp for midnight on the first day of next month&lt;br /&gt;
 $month_end = mktime(0,0,0,$this_month+1,1);&lt;br /&gt;
 &lt;br /&gt;
 while ($day &amp;lt; $month_end) {&lt;br /&gt;
   print strftime('%c',$day); &lt;br /&gt;
   $day += 86400;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;time( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/time'' and &amp;lt;tt&amp;gt;strftime( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/strftime''.&lt;br /&gt;
&lt;br /&gt;
== Using Non-Gregorian Calendars ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to use a non-Gregorian calendar, such as a Julian, Jewish, or French Republican calendar.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
PHP's calendar extension provides conversion functions for working with the Julian calendar, as well as the French Republican and Jewish calendars. To use these functions, the calendar extension must be loaded.&lt;br /&gt;
&lt;br /&gt;
These functions use the Julian day count (which is different than the Julian calendar) as their intermediate format to move information between them.&lt;br /&gt;
&lt;br /&gt;
The two functions &amp;lt;tt&amp;gt;jdtogregorian( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;gregoriantojd( )&amp;lt;/tt&amp;gt; convert between Julian days and the familiar Gregorian calendar:&lt;br /&gt;
&lt;br /&gt;
 $jd = gregoriantojd(3,9,1876);      // March 9, 1876; $jd = 2406323&lt;br /&gt;
 &lt;br /&gt;
 $gregorian = jdtogregorian($jd);    // $gregorian = 3/9/1876&lt;br /&gt;
&lt;br /&gt;
The valid range for the Gregorian calendar is 4714 BCE to 9999 CE.&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
To convert between Julian days and the Julian calendar, use &amp;lt;tt&amp;gt;jdtojulian( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;juliantojd( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 // February 29, 1900 (not a Gregorian leap year)&lt;br /&gt;
 $jd = juliantojd(2,29,1900);      // $jd = 2415092&lt;br /&gt;
 $julian = jdtojulian($jd);        // $julian = 2/29/1900&lt;br /&gt;
 $gregorian = jdtogregorian($jd);  // $gregorian = 3/13/1900&lt;br /&gt;
&lt;br /&gt;
The valid range for the Julian calendar is 4713 BCE to 9999 CE, but since it was created in 46 BCE, you run the risk of annoying Julian calendar purists if you use it for dates before that.&lt;br /&gt;
&lt;br /&gt;
To convert between Julian days and the French Republican calendar, use &amp;lt;tt&amp;gt;jdtofrench( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;frenchtojd( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $jd = frenchtojd(8,13,11);       // 13 floréal XI; $jd = 2379714&lt;br /&gt;
 $french = jdtofrench($jd);       // $french = 8/13/11&lt;br /&gt;
 $gregorian = jdtofregorian($jd); // $gregorian = 5/3/1803; sale of Louisiana to U.S.&lt;br /&gt;
&lt;br /&gt;
The valid range for the French Republican calendar is September 1792 to September 1806, which is small, but since the calendar was only in use from October 1793 to January 1806, it's comprehensive enough.&lt;br /&gt;
&lt;br /&gt;
To convert between Julian days and the Jewish calendar, use &amp;lt;tt&amp;gt;jdtojewish( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;jewishtojd( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $jd = JewishToJD(6,14,5761);      // Adar 14, 5761; $jd = 2451978&lt;br /&gt;
 $jewish = JDToJewish($jd);        // $jewish = 6/14/5761&lt;br /&gt;
 $gregorian = JDToGregorian($jd);  // $gregorian = 3/9/2001&lt;br /&gt;
&lt;br /&gt;
The valid range for the Jewish calendar starts with 3761 BCE (year 1 on the Jewish calendar).&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation for the calendar functions at ''http://www.php.net/calendar''; the history of the Gregorian calendar is explained at ''http://scienceworld.wolfram.com/astronomy/GregorianCalendar.html''.&lt;br /&gt;
&lt;br /&gt;
== Program: Calendar ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;pc_calendar()&amp;lt;/tt&amp;gt; function shown in [[PHP Cookbook/Dates and Times#phpckbk-CHP-3-EX-4|Example 3-4]] prints out a month's calendar, similar to the Unix ''cal'' program. Here's how you can use the function:&lt;br /&gt;
&lt;br /&gt;
 // print the calendar for the current month&lt;br /&gt;
 list($month,$year) = explode(',',date('m,Y'));&lt;br /&gt;
 pc_calendar($month,$year);&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;pc_calendar( )&amp;lt;/tt&amp;gt; function prints out a table with a month's calendar in it. It provides links to the previous and next month and highlights the current day.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-3-EX-4&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-4. pc_calendar( )'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&amp;lt;?php&lt;br /&gt;
function pc_calendar($month,$year,$opts = '') {&lt;br /&gt;
    // set default options //&lt;br /&gt;
    if (! is_array($opts)) { $opts = array(); }&lt;br /&gt;
    if (! isset($opts['today_color'])) { $opts['today_color'] = '#FFFF00'; }&lt;br /&gt;
    if (! isset($opts['month_link'])) {&lt;br /&gt;
        $opts['month_link'] = &lt;br /&gt;
            '&amp;lt;a href=&amp;quot;'.$_SERVER['PHP_SELF'].'?month=%d&amp;amp;year=%d&amp;quot;&amp;gt;%s&amp;lt;/a&amp;gt;';&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    list($this_month,$this_year,$this_day) = split(',',strftime('%m,%Y,%d'));&lt;br /&gt;
    $day_highlight = (($this_month == $month) &amp;amp;&amp;amp; ($this_year == $year));&lt;br /&gt;
    &lt;br /&gt;
    list($prev_month,$prev_year) = &lt;br /&gt;
        split(',',strftime('%m,%Y',mktime(0,0,0,$month-1,1,$year)));&lt;br /&gt;
    $prev_month_link = sprintf($opts['month_link'],$prev_month,$prev_year,'&amp;amp;amp;lt;');&lt;br /&gt;
    &lt;br /&gt;
    list($next_month,$next_year) = &lt;br /&gt;
        split(',',strftime('%m,%Y',mktime(0,0,0,$month+1,1,$year)));&lt;br /&gt;
    $next_month_link = sprintf($opts['month_link'],$next_month,$next_year,'&amp;amp;amp;gt;');&lt;br /&gt;
    &lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;0&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;2&amp;quot; align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
                &amp;lt;td align=&amp;quot;left&amp;quot;&amp;gt;&lt;br /&gt;
                        &amp;lt;?php print $prev_month_link ?&amp;gt;&lt;br /&gt;
                &amp;lt;/td&amp;gt;&lt;br /&gt;
                &amp;lt;td colspan=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php print strftime('%B %Y',mktime(0,0,0,$month,1,$year)); ?&amp;gt;&lt;br /&gt;
                &amp;lt;/td&amp;gt;&lt;br /&gt;
                &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&lt;br /&gt;
                        &amp;lt;?php print $next_month_link ?&amp;gt;&lt;br /&gt;
                &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    $totaldays = date('t',mktime(0,0,0,$month,1,$year));&lt;br /&gt;
 &lt;br /&gt;
    // print out days of the week&lt;br /&gt;
    print '&amp;lt;tr&amp;gt;';&lt;br /&gt;
    $weekdays = array('Su','Mo','Tu','We','Th','Fr','Sa');&lt;br /&gt;
    while (list($k,$v) = each($weekdays)) {&lt;br /&gt;
        print '&amp;lt;td align=&amp;quot;center&amp;quot;&amp;gt;'.$v.'&amp;lt;/td&amp;gt;';&lt;br /&gt;
    }&lt;br /&gt;
    print '&amp;lt;/tr&amp;gt;&amp;lt;tr&amp;gt;';&lt;br /&gt;
    // align the first day of the month with the right week day&lt;br /&gt;
    $day_offset = date(&amp;quot;w&amp;quot;,mktime(0, 0, 0, $month, 1, $year));&lt;br /&gt;
    if ($day_offset &amp;gt; 0) { &lt;br /&gt;
        for ($i = 0; $i &amp;lt; $day_offset; $i++) { print '&amp;lt;td&amp;gt;&amp;amp;amp;nbsp;&amp;lt;/td&amp;gt;'; }&lt;br /&gt;
    }&lt;br /&gt;
    $yesterday = time() - 86400; &lt;br /&gt;
&lt;br /&gt;
    // print out the days&lt;br /&gt;
    for ($day = 1; $day &amp;lt;= $totaldays; $day++) {&lt;br /&gt;
        $day_secs = mktime(0,0,0,$month,$day,$year);&lt;br /&gt;
        if ($day_secs &amp;gt;= $yesterday) {  &lt;br /&gt;
            if ($day_highlight &amp;amp;&amp;amp; ($day == $this_day)) {&lt;br /&gt;
                print sprintf('&amp;lt;td align=&amp;quot;center&amp;quot; bgcolor=&amp;quot;%s&amp;quot;&amp;gt;%d&amp;lt;/td&amp;gt;',&lt;br /&gt;
                              $opts['today_color'],$day);&lt;br /&gt;
            } else {&lt;br /&gt;
                print sprintf('&amp;lt;td align=&amp;quot;center&amp;quot;&amp;gt;%d&amp;lt;/td&amp;gt;',$day);&lt;br /&gt;
            }&lt;br /&gt;
        } else {&lt;br /&gt;
            print sprintf('&amp;lt;td align=&amp;quot;center&amp;quot;&amp;gt;%d&amp;lt;/td&amp;gt;',$day);&lt;br /&gt;
        }&lt;br /&gt;
        $day_offset++;&lt;br /&gt;
&lt;br /&gt;
        // start a new row each week // &lt;br /&gt;
        if ($day_offset == 7) {&lt;br /&gt;
            $day_offset = 0;&lt;br /&gt;
            print &amp;quot;&amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
            if ($day &amp;lt; $totaldays) { print '&amp;lt;tr&amp;gt;'; }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    // fill in the last week with blanks //&lt;br /&gt;
    if ($day_offset &amp;gt; 0) { $day_offset = 7 - $day_offset; }&lt;br /&gt;
    if ($day_offset &amp;gt; 0) { &lt;br /&gt;
        for ($i = 0; $i &amp;lt; $day_offset; $i++) { print '&amp;lt;td&amp;gt;&amp;amp;amp;nbsp;&amp;lt;/td&amp;gt;'; }&lt;br /&gt;
    }&lt;br /&gt;
    print '&amp;lt;/tr&amp;gt;&amp;lt;/table&amp;gt;';&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;pc_calendar( )&amp;lt;/tt&amp;gt; function begins by checking options passed to it in &amp;lt;tt&amp;gt;$opts&amp;lt;/tt&amp;gt;. The color that the current day is highlighted with can be passed as an RGB value in &amp;lt;tt&amp;gt;$opts['today_color']&amp;lt;/tt&amp;gt;. This defaults to &amp;lt;tt&amp;gt;#FFFF00&amp;lt;/tt&amp;gt;, bright yellow. Also, you can pass a &amp;lt;tt&amp;gt;printf( )&amp;lt;/tt&amp;gt;-style format string in &amp;lt;tt&amp;gt;$opts['month_link']&amp;lt;/tt&amp;gt; to change how the links to the previous and next months are printed.&lt;br /&gt;
&lt;br /&gt;
Next, the function sets &amp;lt;tt&amp;gt;$day_highlight&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt; if the month and year for the calendar match the current month and year. The links to the previous month and next month are put into &amp;lt;tt&amp;gt;$prev_month_link&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;$next_month_link&amp;lt;/tt&amp;gt; using the format string in &amp;lt;tt&amp;gt;$opts['month_link']&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;pc_calendar( )&amp;lt;/tt&amp;gt; then prints out the top of the HTML table that contains the calendar and a table row of weekday abbreviations. Using the day of the week returned from &amp;lt;tt&amp;gt;strftime('%w')&amp;lt;/tt&amp;gt;, blank table cells are printed so the first day of the month is aligned with the appropriate day of the week. For example, if the first day of the month is a Tuesday, two blank cells have to be printed to occupy the slots under Sunday and Monday in the first row of the table.&lt;br /&gt;
&lt;br /&gt;
After this preliminary information has been printed, &amp;lt;tt&amp;gt;pc_calendar( )&amp;lt;/tt&amp;gt; loops through all the days in the month. It prints a plain table cell for most days, but a table cell with a different background color for the current day. When &amp;lt;tt&amp;gt;$day_offset&amp;lt;/tt&amp;gt; reaches 7, a week has completed, and a new table row needs to start.&lt;br /&gt;
&lt;br /&gt;
Once a table cell has been printed for each day in the month, blank cells are added to fill out the last row of the table. For example, if the last day of the month is a Thursday, two cells are added to occupy the slots under Friday and Saturday. Last, the table is closed, and the calendar is complete.&lt;br /&gt;
== Notes ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</description>
			<pubDate>Thu, 06 Mar 2008 22:28:45 GMT</pubDate>			<dc:creator>Docbook2Wiki</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:PHP_Cookbook/Dates_and_Times</comments>		</item>
	</channel>
</rss>