The time, please?
8/16/2010 9:30 AM By Jeff BTime is one of the most difficult things to work with in PHP. It comes in so many different formats, in many different orders, and it’s so particular. Thankfully, there are a number of built-in functions in PHP for dealing with time. Some of the most important ones are:
The time() function merely returns the current time measured in seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). While at first this seems rather useless, it actually becomes one of the best ways to deal with comparisons of time, as now we have an integer, and all of our comparisons become simple mathematical relationships and arithmetic equations. This allows for some excellent simple time/date pieces:
<?php
$nextWeek = time() + (7 * 24 * 60 * 60);
// 7 days; 24 hours; 60 mins; 60secs
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
// or using strtotime():
echo 'Next Week: '. date('Y-m-d', strtotime('+1 week')) ."\n";
?>
That example will spit out something like this:
Now: 2005-03-30
Next Week: 2005-04-06
Next Week: 2005-04-06
The date() function returns a string formatted according to given string using either a given integer timestamp or the current time. The timestamp it expects should be in Unix format (e.g. what the time() function returns). An example would be:
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('America/New_York'); // Set to this writer's current timezone
$today = date("F j, Y, g:i a"); // June 28, 2010, 4:39 pm
echo $today;
?>
All of the formatting options for the string are available on the PHP manual page.
The next function mentioned, strtotime(), can convert many English textual date/time pairs into a Unix timestamp. This is most useful for comparisons of times and dates against each other. So you could do something like this:
<?php
$tomorrow = strtotime('+1 day');
$today = time();
if ($tomorrow != $today) {
echo 'Tomorrow never comes.';
}
else {
echo 'Tomorrow is finally here!';
}
?>
The idate() function is a bit confusing if you just read the PHP manual description. It functions similarly to date(), but instead of returning an English formatted date/time string, it only accepts a single format string parameter and returns an integer for that particular item. For example:
<?php
$timestamp = strtotime('1st January 2004'); //1072915200
// this prints the year in four digit format.
echo idate('Y', $timestamp);
?>
This is useful for breaking out pieces of a timestamp and comparing only those pieces. It could be used to group things that happened in the same timeframe together (month, year, etc.)
Finally, here we have a custom function from the GoSquared folks over at their blog Liquidicity:
<?php
/**
* Converts seconds into days:hours:minutes:seconds components
*
* @param int $time - number of seconds
* @return string
*/
function time_quanta($time){
$d = intval(($time / 86400));
$h = intval(($time / 3600) % 24);
$m = intval(($time / 60) % 60);
$s = intval($time % 60);
if(!max($d,$h,$m,$s)) return false;
$st = '';
if($d>0) $st .= ($d < 10?'0'.$d:$d).':';
if($h>0) $st .= ($h < 10?'0'.$h:$h).':';
$st .= ($m < 10?'0'.$m:$m).':';
$st .= ($s < 10?'0'.$s:$s);
return $st;
}
?>
It does just like it says, omitting days and hours if they’re not applicable. Potentially useful, perhaps as a way to represent how long ago something was published in a more human-readable format, or to otherwise compare times in a human-readable fashion.
There are a lot more date functions, but this is the handful that have proven useful recently to this writer. If you’d like to see the whole list, it can be found in the Date/Time Functions article of the PHP manual.
Tags: PHP








Subscribe by RSS