【问题标题】:Get different time to end of the current day获取当前一天结束的不同时间
【发布时间】:2017-09-25 00:34:35
【问题描述】:

好时光。
我怎样才能获得不同的时间到当天结束?
例如,当我运行我的 PHP 脚本时,我想得到这个输出:

19 hours 35 minutes 13 seconds to end of current day

谢谢

【问题讨论】:

标签: php datetime


【解决方案1】:

希望对你有所帮助..

Try this code snippet here

echo remainingTime();
function remainingTime()
{
    $string="";
    $date=  date("Y-m-d H:i:s");
    $endDate= date("Y-m-d")." 23:59:59";
    $remainingSeconds=strtotime($endDate)-strtotime($date);
    $string.=gmdate("H", $remainingSeconds)." hours ";
    $string.=gmdate("i", $remainingSeconds)." minutes ";
    $string.=gmdate("s", $remainingSeconds)." seconds to the end of current day";
    return $string;
}

【讨论】:

    【解决方案2】:

    使用这个概念:

    /**
     * Calculate the remaining time of the day in procedural style
     */
    
    //get current time
    $now = time();
    //get tomorrow's time
    $tomorrow = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y'));
    
    //get the remaining time in second
    $rem =  $tomorrow - $now;
    

    $rem 是以秒为单位的剩余时间。您只需将其除以 60 即可得到多少分钟。再除以 60 得到小时数。

    参考Howto: Calculate remaining time

    【讨论】:

      【解决方案3】:
      <?php
      
      $datetime1 = new DateTime();
      $datetime2 = new DateTime('tomorrow');
      $datetime2->format('Y-m-d H:i:s');
      
      $interval = $datetime1->diff($datetime2);
      echo $interval->format('%h hours %i minutes %s seconds');
      
      ?>
      

      可能的输出是 8 小时 38 分 46 秒

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-14
        • 1970-01-01
        • 1970-01-01
        • 2021-11-09
        • 2015-03-29
        • 2012-04-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多