我试图在午夜和中部时间上午08:00之间执行PHP代码。
我可能没有解释正确:
我有一个表单提交,我想检查用户何时提交表单,如果它在一个时间间隔内,我想做一个额外的操作
如何获得中央标准时间的时间?
我如何根据特定的间隔来检查它?
你的问题不是很具体,所以我将回答我认为你在问什么,但是有很多方法可以做到这一点。
如何获得中央标准时间的时间?
使用PHP的date_default_timezone_set()
,如下:
date_default_timezone_set("America/Chicago");
这里是支持的时区字符串列表。
我如何根据特定的间隔来检查它?
使用cron作业。要开始使用cron
, TheGeekStuff提供了一个很好的教程。简而言之,在您的web服务器上执行以下命令:
$> crontab -e
00 * * * * /usr/local/bin/php /home/john/myscript.php
Edit根据您的评论,您只是想检查脚本执行的时间。您可以将PHP的time()
与strtotime()
结合使用:
date_default_timezone_set("America/Chicago"); // if your server isn't in/set to this timezone already, you're going to need this for proper comparison
if (strtotime("00:00:00") <= time() && time() <= strtotime("8:00:00")) {
// if the time of the script's execution is between midnight and 8am, do something special
} else {
// send to the normal e-mail, it's outside that specific window
}
这个答案使这个问题成为可能。