adding 30 minutes to datetime php/mysql

118,486

Solution 1

If you are using MySQL you can do it like this:

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


For a pure PHP solution use strtotime

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

Solution 2

Try this one

DATE_ADD(datefield, INTERVAL 30 MINUTE)

Solution 3

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')

Solution 4

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.

Solution 5

Use DATE_ADD function

DATE_ADD(datecolumn, INTERVAL 30 MINUTE);
Share:
118,486
someisaac
Author by

someisaac

Updated on July 09, 2022

Comments

  • someisaac
    someisaac almost 2 years

    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

  • kurast
    kurast about 11 years
    I came here looking for a generic way to add to datetime variables, and only this answer worked for me.
  • Taha Khan
    Taha Khan over 2 years
    This is the perfect solution for mysql and php. This should be marked as answer.