简体   繁体   English

将 30 分钟添加到日期时间 php/mysql

[英]adding 30 minutes to datetime php/mysql

I have a datetime field (endTime) in mysql.我在 mysql 中有一个日期时间字段(endTime)。 I use gmdate() to populate this endTime field.我使用 gmdate() 来填充这个 endTime 字段。

The value stored is something like 2009-09-17 04:10:48.存储的值类似于 2009-09-17 04:10:48。 I want to add 30 minutes to this endtime and compare it with current time.我想为此结束时间添加 30 分钟,并将其与当前时间进行比较。 ie) the user is allowed to do a certain task only 30 minutes within his endTime.即)用户只允许在他的 endTime 内的 30 分钟内完成某项任务。 After 30 minutes of his endTime, i should not allow him to do a task.在他结束时间 30 分钟后,我不应该让他做任务。

how can i do that in php?我怎么能在 php 中做到这一点?

i'm using gmdate to make sure there are no zone differences.我正在使用 gmdate 来确保没有区域差异。

Thanks in advance提前致谢

If you are using MySQL you can do it like this:如果你使用MySQL,你可以这样做:

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


For a pure PHP solution use strtotime对于纯 PHP 解决方案,请使用strtotime

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

试试这个

DATE_ADD(datefield, INTERVAL 30 MINUTE)

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). MySQL 有一个名为ADDTIME的函数,用于将两次相加 - 因此您可以在 MySQL 中完成整个操作(前提是您使用的是 >= MySQL 4.1.3)。

Something like (untested):类似的东西(未经测试):

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

Dominc has the right idea, but put the calculation on the other side of the expression. Dominc 有正确的想法,但将计算放在表达式的另一侧。

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.这样做的好处是您只需进行一次 30 分钟的计算,而不是对每一行进行计算。 That also means MySQL can use the index on that column.这也意味着 MySQL 可以使用该列上的索引。 Both of thse give you a speedup.这两者都可以为您提供加速。

使用 DATE_ADD 函数

DATE_ADD(datecolumn, INTERVAL 30 MINUTE);

Get date from MySQL table by adding 30 mins通过添加 30 分钟从 MySQL 表中获取日期

SELECT loginDate, date_add(loginDate,interval 30 minute) as newLoginDate 
FROM `tableName`;

This will result like below这将导致如下

Login Date - 2020-07-22 14:00:00
New Login Date - 2020-07-22 14:30:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM