views:

1102

answers:

4

I have a datetime field (endTime) in mysql. I use gmdate() to populate this endTime field.

The value stored is something like 2009-09-17 04:10:48. I want to add 30 minutes to this endtime and compare it with current time. ie) the user is allowed to do a certain task only 30 minutes within his endTime. After 30 minutes of his endTime, i should not allow him to do a task.

how can i do that in php?

i'm using gmdate to make sure there are no zone differences.

Thanks in advance

+6  A: 

if you are using mysql you can do using

that

select '2008-12-31 23:59:59' + INTERVAL 30 MINUTE

for a pure php solution

use strtotime

strtotime('+ 30 minute',$yourdate);

Cheers

RageZ
+1  A: 

MySQL has a function called ADDTIME for adding two times together - so you can do the whole thing in MySQL (provided you're using >= MySQL 4.1.3).

Something like (untested):

SELECT * FROM my_table WHERE ADDTIME(endTime + '0:30:00') < CONVERT_TZ(NOW(), @@global.time_zone, 'GMT')
Dominic Rodger
+2  A: 

Try this one

DATE_ADD(datefield, INTERVAL 30 MINUTE)
Cem Kalyoncu
+2  A: 

Dominc has the right idea, but put the calculation on the other side of the expression.

SELECT * FROM my_table WHERE endTime < DATE_SUB(CONVERT_TZ(NOW(), @@global.time_zone, 'GMT'), INTERVAL 30 MINUTE)

This has the advantage that you're doing the 30 minute calculation once instead of on every row. That also means MySQL can use the index on that column. Both of thse give you a speedup.

staticsan