首页 > 解决方案 > 如何在php中设置修复时间变量?

问题描述

我想在 php 中为我的 if 和 else 条件设置一个固定时间变量。例如:

$startTime = '08:00:00';
$endTime = '16:00:00';

$totalhrs = $endTime - $startTime;

echo $totalhrs;

有人知道如何在 PHP 中声明时间吗?谢谢您的帮助

标签: php

解决方案


您可以尝试以下功能来检查时间戳。如果您不传递第二个参数,它将评估第一次是否已超过当前时间,否则它将比较第一次与第二次。

Function timeHasPassed($Time, $Time2 = 0) {
        If ($Time2 != 0) {
         $Now = new DateTime($Time2);
        } Else {
          $Now = new DateTime();
        }
        $Then = new DateTime($Time);
        If ($Now > $Then) {
            Return TRUE;
        } Else {
            Return FALSE;
            /*
                You can also use the below code to print out how long is left until the timestamp has passed. Keep in mind, this will return TRUE if tested as a boolean so maybe consider returning a different datatype instead of TRUE if you decide to go this route.
                $Time = new DateTime($Time);
                $Now = new DateTime();
                $Remainder = $Time->diff($Now);
                $Remainder = $Remainder->format("%h hours, %i minutes, and %s seconds!");
                return $Remainder;
            */
        }
    }

推荐阅读