note 74713 modified in function.time by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 04:21:29 +0000 |
| Subject: | note 74713 modified in function.time by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142127@lists.php.net to get a copy of this message | ||
I found Craig's code useful, but wanted the ability to limit the granularity to only show most
significant time periods. there's the bit I adapted:
<?php
function ago ( $epoch, $max_phrases=null ) {
$duration = time() - $epoch;
return seconds2human( $duration > 0 ? $duration : 0,
$max_phrases) . " ago";
}
function seconds2human($epoch, $max_phrases=null ){
// kindly adapted from Craig Francis at http://www.php.net/manual/en/function.time.php#74652
//--------------------------------------------------
// Maths
$sec = $epoch % 60;
$epoch -= $sec;
$minSeconds = $epoch % 3600;
$epoch -= $minSeconds;
$min = ($minSeconds / 60);
$hourSeconds = $epoch % 86400;
$epoch -= $hourSeconds;
$hour = ($hourSeconds / 3600);
$daySeconds = $epoch % 604800;
$epoch -= $daySeconds;
$day = ($daySeconds / 86400);
$week = ($epoch / 604800);
//--------------------------------------------------
// Text
$output = array();
if ($week > 0) {
$output[] = $week . ' week' . ($week != 1 ? 's' : '');
}
if ($day > 0) {
$output[] = $day . ' day' . ($day != 1 ? 's' : '');
}
if ($hour > 0) {
$output[] = $hour . ' hour' . ($hour != 1 ? 's' : '');
}
if ($min > 0) {
$output[] = $min . ' minute' . ($min != 1 ? 's' : '');
}
if ($sec > 0 || $output == '') {
$output[] = $sec . ' second' . ($sec != 1 ? 's' : '');
}
//--------------------------------------------------
// Grammar
if( isset($max_phrases) )
$output = array_slice($output, 0, $max_phrases);
$return = join( ', ', $output);
$return = preg_replace('/, ([^,]+)$/', ' and $1', $return);
//--------------------------------------------------
// Return the output
return $return;
}
?>
--was--
I found Craig's code useful, but wanted the ability to limit the granularity to only show most
significant time periods. there's the bit i adapted:
<?
function ago ( $epoch, $max_phrases=null ) {
$duration = time() - $epoch;
return seconds2human( $duration > 0 ? $duration : 0,
$max_phrases) . " ago";
}
function seconds2human($epoch, $max_phrases=null ){
// kindly adapted from Craig Francis at http://www.php.net/manual/en/function.time.php#74652
//--------------------------------------------------
// Maths
$sec = $epoch % 60;
$epoch -= $sec;
$minSeconds = $epoch % 3600;
$epoch -= $minSeconds;
$min = ($minSeconds / 60);
$hourSeconds = $epoch % 86400;
$epoch -= $hourSeconds;
$hour = ($hourSeconds / 3600);
$daySeconds = $epoch % 604800;
$epoch -= $daySeconds;
$day = ($daySeconds / 86400);
$week = ($epoch / 604800);
//--------------------------------------------------
// Text
$output = array();
if ($week > 0) {
$output[] = $week . ' week' . ($week != 1 ? 's' : '');
}
if ($day > 0) {
$output[] = $day . ' day' . ($day != 1 ? 's' : '');
}
if ($hour > 0) {
$output[] = $hour . ' hour' . ($hour != 1 ? 's' : '');
}
if ($min > 0) {
$output[] = $min . ' minute' . ($min != 1 ? 's' : '');
}
if ($sec > 0 || $output == '') {
$output[] = $sec . ' second' . ($sec != 1 ? 's' : '');
}
//--------------------------------------------------
// Grammar
if( isset($max_phrases) )
$output = array_slice($output, 0, $max_phrases);
$return = join( ', ', $output);
$return = preg_replace('/, ([^,]+)$/', ' and $1', $return);
//--------------------------------------------------
// Return the output
return $return;
}
?>
http://php.net/manual/en/function.time.php