...

View Full Version : math with time



moos3
01-23-2008, 06:31 AM
whats the best way to handle figuring out how long ago it was from this date in the array storage



[DATE] => 22 Jan 2008, 18:23



from the current time?

Mwnciau
01-23-2008, 07:53 AM
$time = strtotime('22 Jan 2008, 18:23');
$time = time() - $time;

echo $time . ' contains the number of seconds since 22 Jan 2008, 18:23';

moos3
01-23-2008, 04:18 PM
now to get it in mins hours seconds days since last

I would do the following:


$str_time = $time;
$m_time = $str_time / 60; // mins how get the reminder to show?
$h_time = $n_time /60; // hours
$d_time = $h_time /24; // days

echo "last entry was: ".$d_time.'days ".$h_time."hours ".$m_time."minutes ".$s_time."seconds ago";



is this even close?

Inigoesdr
01-23-2008, 05:04 PM
$time = strtotime('22 Jan 2008, 18:23');
$time = time() - $time;
$d_time = intval($time / (24 * 60 * 60));
$time -= $d_time * (24 * 60 * 60);
$h_time = intval($time / (60 * 60));
$time -= $h_time * (60 * 60);
$m_time = intval($time / 60);
$time -= $m_time * 60;
$s_time = $time;

echo "last entry was: $d_time days, $h_time hours, $m_time minutes, $s_time seconds ago"; // last entry was: 0 days, 17 hours, 41 minutes, 28 seconds ago

moos3
01-23-2008, 08:25 PM
how do I handle it doing this

last entry was: -13901 days, -22 hours, -54 minutes, 0 seconds ago

its date stamp is 23 Jan 2008, 17:54

Inigoesdr
01-23-2008, 09:26 PM
If you want to account for dates in the future you have to adjust your code:

$strtime = strtotime('23 Jan 2008, 17:54');
if(time() > $strtime)
$time = time() - $strtime;
else
$time = $strtime - time();

$d_time = intval($time / (24 * 60 * 60));
$time -= $d_time * (24 * 60 * 60);
$h_time = intval($time / (60 * 60));
$time -= $h_time * (60 * 60);
$m_time = intval($time / 60);
$time -= $m_time * 60;
$s_time = $time;

if(time() > $strtime)
echo "last entry was: $d_time days, $h_time hours, $m_time minutes, $s_time seconds ago";
else
echo "last entry is in $d_time days, $h_time hours, $m_time minutes, $s_time seconds";
You can use one if/else instead of two like this; this is just an example.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum