我需要检查PHP是否当前时间在当天下午2点之前。
我已经strtotime
在之前的日期进行过此操作,但是这次仅是一个时间,因此显然每天0.00都会重置时间,布尔值将从重置false
为true
。
if (current_time < 2pm) {
// do this
}
if (date('H') < 14) {
$pre2pm = true;
}
有关date函数的更多信息,请参见PHP手册。我使用了以下时间格式化程序:
H =一小时的24小时格式(00到23)
尝试:
if(date("Hi") < "1400") {
}
请参阅: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
你可以打发时间
if (time() < strtotime('2 pm')) {
//not yet 2 pm
}
或也明确传递日期
if (time() < strtotime('2 pm ' . date('d-m-Y'))) {
//not yet 2 pm
}
使用24小时来解决问题,如下所示:
$time = 1400;
$current_time = (int) date('Hi');
if($current_time < $time) {
// do stuff
}
因此2PM等于24小时内的14:00。如果从时间中删除冒号,则可以在比较中将其评估为整数。
有关date函数的更多信息,请参见PHP手册。我使用了以下时间格式化程序:
H =一小时的24小时格式(00到23)
i =以零开头的分钟(00到59)
您没有告诉我们您正在运行哪个版本的PHP,但是假设它是PHP 5.2.2+,那么您应该能够做到:
$now = new DateTime();
$twoPm = new DateTime();
$twoPm->setTime(14,0); // 2:00 PM
然后问:
if ( $now < $twoPm ){ // such comparison exists in PHP >= 5.2.2
// do this
}
否则,如果您使用的是旧版本之一(例如5.0),则应该可以解决问题(并且更加简单):
$now = time();
$twoPm = mktime(14); // first argument is HOUR
if ( $now < $twoPm ){
// do this
}
如果要检查时间是否在2.30 pm之前,可以尝试以下代码段。
if (date('H') < 14.30) {
$pre2pm = true;
}else{
$pre2pm = false;
}
试试看
if( time() < mktime(14, 0, 0, date("n"), date("j"), date("Y")) ) {
// do this
}
该函数将通过接受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;
}
使用time()
,date()
并且strtotime()
功能:
if(time() > strtotime(date('Y-m-d').' 14:00') {
//...
}
本文地址:http://php.askforanswer.com/phpjianchadangqianshijianshifouzaizhidingshijianzhiqian.html
文章标签:date , php , strtotime
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
文章标签:date , php , strtotime
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
评论已关闭!