PHP 检查当前时间是否在指定时间之前
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8475019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Check if current time is before specified time
提问by Adam Moss
I need to check in PHP if the current time is before 2pm that day.
如果当前时间在当天下午 2 点之前,我需要检查 PHP。
I've done this with strtotime
on dates before, however this time it's with a time only, so obviously at 0.00 each day the time will reset, and the boolean will reset from false
to true
.
我之前strtotime
在日期上做过这个,但是这次它只有时间,所以很明显每天 0.00 时间将重置,布尔值将从 重置false
为true
。
if (current_time < 2pm) {
// do this
}
回答by Tom
if (date('H') < 14) {
$pre2pm = true;
}
For more information about the date function please see the PHP manual. I have used the following time formatter:
有关日期函数的更多信息,请参阅 PHP 手册。我使用了以下时间格式化程序:
H = 24-hour format of an hour (00 to 23)
H = 小时的 24 小时格式(00 到 23)
回答by Merlyn Morgan-Graham
Try:
尝试:
if(date("Hi") < "1400") {
}
See: http://php.net/manual/en/function.date.php
见:http: //php.net/manual/en/function.date.php
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
回答by JohnP
You could just pass in the time
你可以打发时间
if (time() < strtotime('2 pm')) {
//not yet 2 pm
}
Or pass in the date explicitly as well
或者也明确传入日期
if (time() < strtotime('2 pm ' . date('d-m-Y'))) {
//not yet 2 pm
}
回答by Treffynnon
Use 24 hour time to get round the problem like so:
使用 24 小时来解决问题,如下所示:
$time = 1400;
$current_time = (int) date('Hi');
if($current_time < $time) {
// do stuff
}
So 2PM equates to 14:00 in 24 hour time. If we remove the colon from the time then we can evaluate it as an integer in our comparison.
所以 2PM 相当于 24 小时内的 14:00。如果我们从 time 中删除冒号,那么我们可以在比较中将其评估为整数。
For more information about the date function please see the PHP manual. I have used the following time formatters:
有关日期函数的更多信息,请参阅 PHP 手册。我使用了以下时间格式化程序:
H = 24-hour format of an hour (00 to 23)
i = Minutes with leading zeros (00 to 59)
H = 小时的 24 小时格式(00 到 23)
i = 带前导零的分钟数(00 到 59)
回答by Jovan Perovic
You haven't told us which version of PHP you're running, although, assuming it's PHP 5.2.2+ than you should be able do it like:
您还没有告诉我们您正在运行哪个版本的 PHP,但是,假设它是 PHP 5.2.2+,您应该可以这样做:
$now = new DateTime();
$twoPm = new DateTime();
$twoPm->setTime(14,0); // 2:00 PM
then just ask:
那么就问:
if ( $now < $twoPm ){ // such comparison exists in PHP >= 5.2.2
// do this
}
otherwise, if you're using one of older version (say, 5.0) this should do the trick (and is much simplier):
否则,如果您使用的是旧版本之一(例如 5.0),这应该可以解决问题(并且更简单):
$now = time();
$twoPm = mktime(14); // first argument is HOUR
if ( $now < $twoPm ){
// do this
}
回答by Udeshika Sewwandi
If you want to check whether the time is before 2.30 pm ,you can try the following code segment .
如果您想检查时间是否在下午 2.30 之前,您可以尝试以下代码段。
if (date('H') < 14.30) {
$pre2pm = true;
}else{
$pre2pm = false;
}
回答by FueledPublishing
This function will check if it's between hours in EST by accepting 2 params, arrays with the hour and am/pm...
此函数将通过接受 2 个参数、带有小时和上午/下午的数组来检查它是否在 EST 的几个小时之间...
/**
* Check if between hours array(12,'pm'), array(2,'pm')
*/
function is_between_hours($h1 = array(), $h2 = array())
{
date_default_timezone_set('US/Eastern');
$est_hour = date('H');
$h1 = ($h1[1] == 'am') ? $h1[0] : $h1[0]+12;
$h1 = ($h1 === 24) ? 12 : $h1;
$h2 = ($h2[1] == 'am') ? $h2[0] : $h2[0]+12;
$h2 = ($h2 === 24) ? 12 : $h2;
if ( $est_hour >= $h1 && $est_hour <= ($h2-1) )
return true;
return false;
}
回答by Aryeh Armon
Use time()
, date()
and strtotime()
functions:
使用time()
,date()
和strtotime()
函数:
if(time() > strtotime(date('Y-m-d').' 14:00') {
//...
}
回答by Repox
Try with
试试
if( time() < mktime(14, 0, 0, date("n"), date("j"), date("Y")) ) {
// do this
}