Page 1 of 1

getting number of minutes that went by

Posted: Tue May 30, 2006 11:46 pm
by psychotomus
how can I get the number of minutes that have passed.

i tried this.
where LastTurnUpdate is time(). i get 0

Code: Select all

$date1 = strtotime($row['LastTurnUpdate']); 
$date2 = strtotime(time());
print $date2 - $date1;

Posted: Tue May 30, 2006 11:59 pm
by feyd
strtotime() is useless here.

Finding the difference between the two individual marks will tell you the number of seconds which have passed between them. It's simple math from there.

Posted: Wed May 31, 2006 12:04 am
by psychotomus

Code: Select all

$date1 = $row['LastTurnUpdate']; 
$date2 = time();
$next_turn =  ($date2 - $date1) / 60;

Posted: Wed May 31, 2006 12:07 am
by Weirdan
if $row['LastTurnUpdate'] is unix timestamp:

Code: Select all

echo (time() - $row['LastTurnUpdate']) / 60;
if $row['LastTurnUpdate'] is mysql timestamp:

Code: Select all

function m2u_timestamp($m) {
   preg_match("#^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$#", $m, $matches);
   return mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
}
echo (time() - m2u_timestamp($row['LastTurnUpdate'])) / 60;
if $row['LastTurnUpdate'] is date string:

Code: Select all

echo (time() - strtotime($row['LastTurnUpdate'])) / 60;

Posted: Wed May 31, 2006 10:00 am
by pickle
Weirdan wrote: if $row['LastTurnUpdate'] is mysql timestamp:

Code: Select all

function m2u_timestamp($m) {
   preg_match("#^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$#", $m, $matches);
   return mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
}
echo (time() - m2u_timestamp($row['LastTurnUpdate'])) / 60;
if $row['LastTurnUpdate'] is a mysql timestamp, probably the easiest thing to do would be to modify the query to wrap LastTurnUpdate in UNIX_TIMESTAMP().

Posted: Wed May 31, 2006 10:02 am
by Weirdan
pickle wrote: if $row['LastTurnUpdate'] is a mysql timestamp, probably the easiest thing to do would be to modify the query to wrap LastTurnUpdate in UNIX_TIMESTAMP().
Sure. But I'd imagine there are cases when it's not possible...

Posted: Wed May 31, 2006 10:08 am
by pickle
True enough.