PHP Dates and Times
Converting Time and Date Parts to an Epoch Timestamp
Problem
You want to know what epoch timestamp corresponds to a set of time and date parts.Solution
Example Getting a specific epoch timestamp// 7:45:03 PM on March 10, 1975, local time
// Assuming your "local time" is US Eastern time
$then = mktime(19,45,3,3,10,1975);
Use gmmktime(), as in Example, if your time and date parts are in GMT.
Example Getting a specific GMT-based epoch timestamp
// 7:45:03 PM on March 10, 1975, in GMT
$then = gmmktime(19,45,3,3,10,1975);
Use DateTime::createFromFormat(), as in Example, if your time and date parts are in a formatted time string.
Example Getting a specific epoch timestamp from a formatted time string
// 7:45:03 PM on March 10, 1975, in a particular timezone
$then = DateTime::createFromFormat(DateTime::ATOM, "1975-03-10T19:45:03-04:00");
Discussion
The functions mktime() and gmmktime() each take a date and time’s parts (hour, minute, second, month, day, year) and return the appropriate Unix epoch timestamp. The components are treated as local time by mktime(), while gmmktime() treats them as a date and time in UTC.
In Example, $stamp_future is set to the epoch timestamp for 3:25 P.M. on December 3, 2024. The epoch timestamp can be fed back to date() to produce a formatted time string.
Example Working with epoch timestamps
date_default_timezone_set('America/New_York');
// $stamp_future is 1733257500
$stamp_future = mktime(15,25,0,12,3,2024);
// $formatted is '2024-12-03T15:25:00-05:00'
$formatted = date('c', $stamp_future);
Because the calls to mktime() in Example were made with the time zone set to America/New_York, using gmmktime() instead produces epoch timestamps that are 18,000 seconds (five hours) smaller, as shown in Example.
Example Epoch timestamps and gmmktime()
date_default_timezone_set('America/New_York');
// $stamp_future is 1733239500, whch is 18000
// smaller than 1733257500
$stamp_future = gmmktime(15,25,0,12,3,2024);
The createFromFormat() method of the DateTime class behaves more flexibly. Instead of accepting already-chopped-up time parts, you give it a formatted time or date string and tell it the structure of that string. It then decomposes the parts properly and calculates the correct timestamp.
Table Format characters for DateTime::createFromFormat( )
Character Meaning
space or tab
# Any one of the separation bytes ;, :, /, ., ,, -, (, )
;, :, /, ., ,, -, (, ) Literal character
? Any byte (not a character, just one byte)
* Any number of bytes until the next digit or separation character
! Resetall fieldsto“startof Unixepoch”values(without this,any unspecified fields will be
set to the currrent date/time)
| Reset any unparsed fields to “start of Unix epoch” values
+ Treat unparsed trailing data as a warning rather than an error
________________________________________________________________________________
Example shows how DateTime::createFromFormat() can be used to get time parts out of a larger string.
Example Using DateTime::createFromFormat( )
$text = "Birthday: May 11, 1918.";
$when = DateTime::createFromFormat("*: F j, Y.|", $text);
// $formatted is "Saturday, 11-May-18 00:00:00 UTC"
$formatted = $when->format(DateTime::RFC850);
No comments:
Post a Comment