如何在php中设置修复时间变量?

时间:2018-08-07 00:53:26

标签: php

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

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

$totalhrs = $endTime - $startTime;

echo $totalhrs;

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

3 个答案:

答案 0 :(得分:0)

$startTime  =   strtotime('08:00:00');
$endTime    =   strtotime('16:00:00');

$totalhrs   =   ($endTime - $startTime) / 3600;
echo $totalhrs;

答案 1 :(得分:0)

在这种情况下,您可以使用datetime对象

mongodb.so

答案 2 :(得分:0)

您可以尝试使用以下功能检查时间戳。如果您没有将第二个参数传递给它,它将评估第一个时间是否已超过当前时间,否则它将把第一时间与第二个时间进行比较。

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;
            */
        }
    }