如何使用php生成一个随机时间间隔并将其添加到mysql datetime? [英] How do I generate a random time interval and add it to a mysql datetime using php?
本文介绍了如何使用php生成一个随机时间间隔并将其添加到mysql datetime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在mysql表中有很多行,其中datetime的格式为:
I have many rows in mysql table with datetime's in the format of:
2008-12-08 04:16:51 etc
我想生成一个介于30秒和3天之间的随机时间间隔,并将它们添加到上述时间中.
I'd like to generate a random time interval of anywhere between 30 seconds, and 3 days and add them to the time above.
a)如何生成30到3天之间的随机时间?
a) how do I generate a random time between 30 and 3 days?
b)如何将此时间添加到上面的日期时间格式?
b) how do I add this time to the date time format above?
我想我需要做一个循环以提取所有信息,在php中进行数学运算,然后更新该行...
I imagine i need to do a loop to pull out all the info, do the math in php, and then update the row...
有什么想法吗?
推荐答案
更简便的方法.
$new_date = date('Y-m-d h:i:s', strtotime('2008-12-08 04:16:51 +'.rand(30, 60 * 60 * 24 * 3).' seconds'));
这篇关于如何使用php生成一个随机时间间隔并将其添加到mysql datetime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文