Main Content
Time calculation in PHP Archive - Originally posted on "The Horse's Mouth" - 2005-07-08 11:25:58 - Graham Ellis
Looking to calculate using time in PHP? Been provided with some timestamps such as
[23/Jun/2005:00:06:54 -0700]? (That example is from an Apache Log file).
Method:
- extract the relevant parts of the date from the string
- convert months to a number
- convert the numbers to seconds from Midnight, 1.1.1970
- you can calculate using the resulting figure!
function getwhen($dateinfo) {
$month = array("Jan" => 1,"Feb" => 2,"Mar" => 3, "Apr" => 4,
"May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8,
"Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12);
# Note - hour, minute, second, month, day, year order!
$ts = mktime($dateinfo[4],$dateinfo[5],$dateinfo[6],
$month[$dateinfo[2]],$dateinfo[1],$dateinfo[3]);
# Time zone correction
$ts -= $dateinfo[7] * 3600;
return $ts;
}
ereg('(..)/(...)/(....):(..):(..):(..) (...)',$first,$startdate);
$timingfrom = getwhen ($startdate);
PHP's date function can be used to convert it the opposite direction - from a value in seconds from 1.1.1970 into a string.
Some other articles
H999 - Additional PHP Material Image indexer / thumbnail display scripts in PHP Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON Arrays of arrays - or 2D arrays. How to program tables. Exception handling in PHP If nothing, make it nothing. Extra PHP Examples PHP Techniques - a workshop Flipping images on your web page Script to present commonly used images - PHP Copyright and theft of images, bandwidth and members. More PHP sample and demonstration programs Ordnance Survey Grid Reference to Latitude / Longitude Converting from postal address to latitude / longitude Controlling and labelling Google maps via PHP PHP Standalone - keyboard to screen Drawing hands on a clock face - PHP Drawing dynamic graphs in PHP Sorting people by name in PHP Parallel processing in PHP Dates, times, clickable diarys in PHP Display an image from a MySQL database in a web page via PHP Syntax checking in PHP Reporting on the 10 largest files or 10 top scores PHP - a team member leaves Check your user is human. Have him retype a word in a graphic Hot answers in PHP Presentation, Business and Persistence layers in Perl and PHP PHP Image viewing application PHP - setting sort order with an associative array Merging pictures using PHP and GD Running a Perl script within a PHP page Double Dollars in PHP Stand alone PHP programs This article the array returned by preg_match_all More maps Ordnance Survey - using a 'Get a map' What and why for the epoch PHP and natural sorting H112 - Further Web Page and Network Handling Moving from mysql to mysqli - simple worked example Passing variable between PHP pages - hidden fields, cookies and sessions Multiple page web applications - maintaining state - PHP Telling which ServerAlias your visitor used - useful during merging domains Easy session example in PHP - keeping each customers data apart 3 digit HTTP status codes - what are they, which are most common, which should be a concern? Sending out an email containing HTML from within a PHP page Downloading a report from the web for further local analysis Uploading a document or image to its own URL via a browser How to build a test harness into your PHP Shipping a test harness with your class in PHP http, https and ajp - comparison and choice Downloading data for use in Excel (from PHP / MySQL) Keeping staff up to date on hotel room status Script to present commonly used images - PHP PHP / Web 2 logging Single login and single threaded models - Java and PHP Copyright and theft of images, bandwidth and members. Simple page password protection - PHP .php or .html extension? Morally Static Pages PHP header() function - uses and new restrictions Updating a page strictly every minute (PHP, Perl) Improving searches - from OR to AND? PHP Image upload script Passing GET parameters through Apache mod_rewrite Global, Superglobal, Session variables - scope and persistance in PHP Of course I'll tell you by email Image maps for navigation - a straightforward example Hot answers in PHP Finding the language preference of a web site visitor Adding PHP tags to an old cgi program PHP - setting sort order with an associative array Using PHP to output images, XML, Style sheets, etc Morning image, afternoon image Daily Image Santafied Setting the file name for a downloaded document Accessing a page via POST from within a PHP script Server side scripting of styles to suit the browser Caching an XML feed Reading a news or blog feed (RSS) in your PHP page What brings people to my web site? This article Sudoku helper or sudoku cheat Spotting a denial of service attack What language is this written in? When to use Frames