php大于一定时间 [英] php greater than certain time

查看:31
本文介绍了php大于一定时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的函数来根据一天中的时间输出 2 行不同的文本.我希望它在下午 4 点之后说 - 次日交货将在次日处理.

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.

到目前为止我已经写了这个:

I have wrote this so far:

<?php

   $currentTime = time() + 3600;
   echo date('H:i',$currentTime);                 

?>   

但是由于date函数返回一个字符串,我不确定如何使用IF语句来检查时间是否大于16:00.

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.

推荐答案

应该做的

if (((int) date('H', $currentTime)) >= 16) {
  // .. do something
}

因为 PHP 是弱类型的,所以可以省略 (int)-casting.

Because PHP is weak-typed you can omit the (int)-casting.

作为旁注:如果你命名一个变量 $currentTime,你不应该向它添加 1 小时,因为它不再是当前时间,而是未来一小时的时间;) 在所有

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

if (date('H') >= 16) { /* .. */ }

这篇关于php大于一定时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆