Date & Time Manipulation in PHP
Terminology
date('H:i:s') ¶ definition: 'Current time in 00:00:00 format.'
date('Y/m/d') ¶ Current date in 0000/00/00 format.
strtotime(<string>) ¶ definition: 'Convert date string or time length string to Unix timestamp.'
Facts, Thoughts and Opinions
PHP "ago" function
function ago($time) { $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); $lengths = array("60","60","24","7","4.35","12","10"); $now = time(); $difference = $now - $time; $tense = "ago"; for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { $difference /= $lengths[$j]; } $difference = round($difference); if($difference != 1) { $periods[$j].= "s"; } return "$difference $periods[$j] 'ago' "; }
PHP time duration to string
function secondsToString($seconds){
$bit = array(
'h' => floor($seconds/3600),
'm' => floor($seconds/60) % 60,
's' => $seconds % 60
);
foreach($bit as $key => $value)
if ($value > 0) $ret[] = $value . $key;
return join('', $ret);
}
Images
[[/div]]