php 大于一定时间

发布于 2024-11-15 17:33:23 字数 278 浏览 3 评论 0原文

我正在尝试创建一个简单的函数来根据一天中的时间输出两行不同的文本。我想说的是下午 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

℡寂寞咖啡 2024-11-22 17:33:24

我一天用它更新了 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

I used this for updating something 3 times throughout the day...

$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
谈下烟灰 2024-11-22 17:33:24
// Happy coding 
$isCurrentTimeGreater = strtotime('now') > strtotime('2022-10-13 08:15 am');

echo $timeDiff;
// Happy coding 
$isCurrentTimeGreater = strtotime('now') > strtotime('2022-10-13 08:15 am');

echo $timeDiff;
旧竹 2024-11-22 17:33:24

您的问题已得到解答,但如果您使用 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.e estimated_completion_time should be greater than today means its in future.

晌融 2024-11-22 17:33:23

应该这样做

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

因为 PHP 是弱类型的,所以您可以省略 (int) 转换。

作为旁注:如果你命名一个变量 $currentTime,你不应该给它添加 1 小时,因为那样它就不再是当前时间,而是未来一小时的时间;) 根本

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

Should do it

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

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

if (date('H') >= 16) { /* .. */ }
岛歌少女 2024-11-22 17:33:23
if ($currentTime > strtotime('16:00:00')) {
    // whatever you have to do here
}
if ($currentTime > strtotime('16:00:00')) {
    // whatever you have to do here
}
听你说爱我 2024-11-22 17:33:23

使用 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".

情痴 2024-11-22 17:33:23

比较时间戳。

if (strtotime($date) < strtotime('16:00'))

Compare the timestamps.

if (strtotime($date) < strtotime('16:00'))
往日情怀 2024-11-22 17:33:23
if(mktime(16, 0, 0) < time()) {
    echo "Next day delivery will be processed the following day.";
} else {
   echo "Will be processed today.";
}
if(mktime(16, 0, 0) < time()) {
    echo "Next day delivery will be processed the following day.";
} else {
   echo "Will be processed today.";
}
梦醒灬来后我 2024-11-22 17:33:23

如果您想要更高的准确性,也许有一个小小的改进:

if ( (int) date('Hi', $currentTime)  > 1600 ) {
    // .. do something
}

Maybe a small improvement, if you want some more accuracy:

if ( (int) date('Hi', $currentTime)  > 1600 ) {
    // .. do something
}
此岸叶落 2024-11-22 17:33:23

您还可以检查两个日期之间的差异:-

<?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)
?> 

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)
?> 
谁许谁一生繁华 2024-11-22 17:33:23

试试这个

    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";

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文