views:

54

answers:

4

Hi all, how can I make a conditional statement like if date("Y-m-d h:i:s"); is more than 30 seconds after date("Y-m-d h:i:s");.

I've previously used something like date("Y-m-d h:i:s"); < date("Y-m-d h:i:s"); + 30, however this doesn't seem to work.

Help?

+4  A: 

Use strtotime() to convert them to UNIX time.

E.g.:

if(strtotime($date2) - strtotime($date1) > 30) {
    // $date2 is more than 30 seconds after $date1
}

or

if(abs(strtotime($date2) - strtotime($date1)) > 30) {
    // $dates are more than 30 second apart
}
Felix Kling
A: 

Use unixtime, instead. Just convert your date("Y-m-d h:i:s"); to date("U") or just time(). strototime() could help you with this. It shows the time in seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Then this method $unixtime + 30 will work fine as well ;)

Vladimiroff
+1  A: 

Use strtotime() to convert both dates to a unix timestamp, then add the wanted amount of seconds to the second and do an integer comparison.

bluebrother
+1  A: 

It looks like you are trying to compare database date fields (like mysql DATETIME). Use the database systems date and time functions to compare date values (like DATE_ADD() or + and with simple < or > in MySQL).

Progman