getting number of minutes that went by

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

getting number of minutes that went by

Post 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;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

Code: Select all

$date1 = $row['LastTurnUpdate']; 
$date2 = time();
$next_turn =  ($date2 - $date1) / 60;
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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;
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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().
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

True enough.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply