Get time elapsed from a date to current date

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
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Get time elapsed from a date to current date

Post by Cruzado_Mainfrm »

I want to get the time that have elapsed from a date
and check if 48 hours have elapsed then evaluate to true, if not, false

Code: Select all

$firstdate = strtotime("1/1/2001 13:0:0");  //ex, this date is just taken from a DB so it could be anything
$currentdate = date("H:m:s");
$result = ???? in
thnx in advance
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Timestamps are likely to be useful here:

Code: Select all

$date1 = mktime(13, 0, 0, 1, 1, 2001);
$date2 = mktime(-48); // 48 hours ago

if ($date1 <= $date2) {
    // date is at least 48 hours ago
} else {
    // date is less than 48 hours ago
}
Mac
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

thnx i guess that would help :D
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

wouldn't that be like:

Code: Select all

<?php

$date1 = mktime(13, 0, 0, 1, 1, 2001); 
$date2 = mktime(date("H")-48); // 48 hours ago   <--change

if ($date1 <= $date2) { 
    // date is at least 48 hours ago 
} else { 
    // date is less than 48 hours ago 
}

?>
any ideas? i think i have to get the current time, and subtract 48 hours from it, cuz when i use -48 alone it sends me one day back...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

That's weird, I'm sure it worked for me :? . Let me have a test again.

Mac
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Stick with your way, someone will be able to tell me (hopefully since I'm having a brain fart) why my way didn't work as expected (didn't take me back exactly 48 hours either).

Mac
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

thankx anyway, that gave me the idea :)
Post Reply