Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding 30 minutes to datetime php/mysql

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

like image 416
someisaac Avatar asked Sep 17 '09 05:09

someisaac


People also ask

How to add 30 minutes to current time in MySQL?

To add minutes to a datetime you can use DATE_ADD() function from MySQL. In PHP, you can use strtotime(). select date_add(yourColumnName,interval 30 minute) from yourTableName; To use the above syntax, let us create a table.

What is DATE_ADD in MySQL?

The DATE_ADD() function adds a time/date interval to a date and then returns the date.

What is interval in MySQL?

MySQL interval is an operator, which is based on the binary search algorithm to search the items and returns the value from 0 to N. It is mainly used to calculate the date and time values. We can use the following syntax to create an interval value: INTERVAL expr unit.


2 Answers

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); 
like image 90
RageZ Avatar answered Sep 21 '22 00:09

RageZ


Try this one

DATE_ADD(datefield, INTERVAL 30 MINUTE) 
like image 30
Cem Kalyoncu Avatar answered Sep 23 '22 00:09

Cem Kalyoncu