php 大于一定时间
我正在尝试创建一个简单的函数来根据一天中的时间输出两行不同的文本。我想说的是下午 4 点之后 - 次日交货将在第二天进行处理。
到目前为止我已经这样写了:
<?php
$currentTime = time() + 3600;
echo date('H:i',$currentTime);
?>
但是,由于日期函数返回一个字符串,我不确定如何使用 IF 语句来检查时间是否大于 16:00。
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);
?>
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我一天用它更新了 3 次东西......
I used this for updating something 3 times throughout the day...
您的问题已得到解答,但如果您使用 laravel,我将添加一些额外的信息,然后在您的 formRequest Validation 中您可以添加
'estimated_completion_time' =>; 'required|after:today',
规则强制创建日期,即estimated_completion_time
应该大于今天,这意味着它是未来的。your question is answered but i will add some extra information if you are using laravel then in your formRequest Validation you can add
'estimated_completion_time' => 'required|after:today',
rule to enforce that the date created i.eestimated_completion_time
should be greater than today means its in future.应该这样做
因为 PHP 是弱类型的,所以您可以省略
(int)
转换。作为旁注:如果你命名一个变量
$currentTime
,你不应该给它添加 1 小时,因为那样它就不再是当前时间,而是未来一小时的时间;) 根本Should do it
Because PHP is weak-typed you can omit the
(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使用
date('H:i:s')
获取字符串格式的时间,并将其与“16:00:00”进行比较。Use
date('H:i:s')
to get the time in a string format and compare it with "16:00:00".比较时间戳。
Compare the timestamps.
如果您想要更高的准确性,也许有一个小小的改进:
Maybe a small improvement, if you want some more accuracy:
您还可以检查两个日期之间的差异:-
you can also check the difference between two Dates:-
试试这个
Try this