View Full Version : how many minutes
sir pannels
07-06-2007, 03:52 PM
hi all,
im trying to work out how many minutes have past and how many remain since the last datetime stamp...
does anyone know of any pre-existing functions for this before I reivent the wheel?
Many thanks
mlseim
07-06-2007, 04:13 PM
This is from this website: http://us2.php.net/time
<?php
define('INT_SECOND', 1);
define('INT_MINUTE', 60);
define('INT_HOUR', 3600);
define('INT_DAY', 86400);
define('INT_WEEK', 604800);
function get_formatted_timediff($then, $now = false)
{
$now = (!$now) ? time() : $now;
$timediff = ($now - $then);
$weeks = (int) intval($timediff / INT_WEEK);
$timediff = (int) intval($timediff - (INT_WEEK * $weeks));
$days = (int) intval($timediff / INT_DAY);
$timediff = (int) intval($timediff - (INT_DAY * $days));
$hours = (int) intval($timediff / INT_HOUR);
$timediff = (int) intval($timediff - (INT_HOUR * $hours));
$mins = (int) intval($timediff / INT_MINUTE);
$timediff = (int) intval($timediff - (INT_MINUTE * $mins));
$sec = (int) intval($timediff / INT_SECOND);
$timediff = (int) intval($timediff - ($sec * INT_SECOND));
$str = '';
if ( $weeks )
{
$str .= intval($weeks);
$str .= ($weeks > 1) ? ' weeks' : ' week';
}
if ( $days )
{
$str .= ($str) ? ', ' : '';
$str .= intval($days);
$str .= ($days > 1) ? ' days' : ' day';
}
if ( $hours )
{
$str .= ($str) ? ', ' : '';
$str .= intval($hours);
$str .= ($hours > 1) ? ' hours' : ' hour';
}
if ( $mins )
{
$str .= ($str) ? ', ' : '';
$str .= intval($mins);
$str .= ($mins > 1) ? ' minutes' : ' minute';
}
if ( $sec )
{
$str .= ($str) ? ', ' : '';
$str .= intval($sec);
$str .= ($sec > 1) ? ' seconds' : ' second';
}
if ( !$weeks && !$days && !$hours && !$mins && !$sec )
{
$str .= '0 seconds ago';
}
else
{
$str .= ' ago';
}
return $str;
}
?>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.