php php大于一定时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6370538/
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 greater than certain time
提问by Glynn
I am trying to make a simple function to output 2 different lines of text depending on the time of the day. I want it to say after 4pm - Next day delivery will be processed the following day.
我正在尝试制作一个简单的函数来根据一天中的时间输出 2 行不同的文本。我希望它在下午 4 点之后说 - 次日交货将在次日处理。
I have wrote this so far:
到目前为止,我已经写了这个:
<?php
$currentTime = time() + 3600;
echo date('H:i',$currentTime);
?>
However as the date function returns a string, I am unsure of how to use an IF statement to check whether the time is greater than 16:00.
但是由于 date 函数返回一个字符串,我不确定如何使用 IF 语句来检查时间是否大于 16:00。
回答by KingCrunch
Should do it
应该做
if (((int) date('H', $currentTime)) >= 16) {
// .. do something
}
Because PHP is weak-typed you can omit the (int)
-casting.
因为 PHP 是弱类型的,所以您可以省略(int)
-casting。
As a sidenote: If you name a variable $currentTime
, you shouldn't add 1 hour to it, because then its not the current time anymore, but a time one hour in the future ;) At all
作为旁注:如果你命名一个变量$currentTime
,你不应该向它添加 1 小时,因为它不再是当前时间,而是未来一小时的时间;) 完全
if (date('H') >= 16) { /* .. */ }
回答by mkilmanas
if ($currentTime > strtotime('16:00:00')) {
// whatever you have to do here
}
回答by Balanivash
Use date('H:i:s')
to get the time in a string format and compare it with "16:00:00".
用于date('H:i:s')
以字符串格式获取时间并将其与“16:00:00”进行比较。
回答by moteutsch
if(mktime(16, 0, 0) < time()) {
echo "Next day delivery will be processed the following day.";
} else {
echo "Will be processed today.";
}
回答by 3eighty
Maybe a small improvement, if you want some more accuracy:
如果您想要更高的准确性,也许是一个小小的改进:
if ( (int) date('Hi', $currentTime) > 1600 ) {
// .. do something
}
回答by Jon Harvey
I used this for updating something 3 times throughout the day...
我用它来全天更新 3 次...
$my_time= strtotime('now')-18000;//Now. My servers timezone is 5hrs ahead.
$my_day= strtotime('today')-86400; //today midnight central time.
$my_eve= $my_day +54000; //3pm central
if ($my_time > $my_day + 32400 && $my_time < $my_eve){
echo date('F j, Y, g:i a', $my_day + 32400); //9am
}elseif ($my_time > $my_eve){
echo date('F j, Y, g:i a', $my_eve); //3pm
}else{
echo date('F j, Y, g:i a', $my_day);} //midnight
回答by Vishal Thakur
you can also check the difference between two Dates:-
您还可以检查两个日期之间的差异:-
<?php
$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");
var_dump($date1 == $date2); //bool(false)
var_dump($date1 < $date2); //bool(true)
var_dump($date1 > $date2); //bool(false)
?>
回答by kamal
Try this
尝试这个
function checkTime($time1,$time2)
{
$start = strtotime($time1);
$end = strtotime($time2);
if ($start-$end > 0)
return 1;
else
return 0;
}
$currentTime = time() + 3600;
$time1 = date('H:i',$currentTime);
$time2 = '16:00';
if(checkTime($time1,$time2))
echo "First parameter is greater";
else
echo "Second parameter is greater";
回答by Denis de Bernardy
Compare the timestamps.
比较时间戳。
if (strtotime($date) < strtotime('16:00'))