如何使用PHP减去两个时间值?

时间:2016-11-27 19:41:05

标签: javascript php html mysql

我几乎完成了在互联网上找到的所有内容。过去两天我一直在努力,但无法得到满意的答案。 我只想减去两次。 例如22:00:00 - 00:30:00 = 21:30:00

$hourToEatLastMeal = strtotime('00:30:00');
$hourToEatWorkout = strtotime('01:30:00');
$hourBetweenMeal = strtotime('07:30:00');
$lastMealRequested = strtotime($row['lastMeal']); //22:00:00 //value getting from query.

$startEating = $lastMealRequested - $hourToEatLastMeal ;//21:30:00 (want this answer)
$timeToEat = $startEating- $hourBetweenMeal; //14:00:00 (want this answer)
$whenToWorkout = $timeToEat - $hourToEatWorkout; //12:30:00 (want this answer)

$whenToWorkout = date("H:i:s",($whenToWorkout));//12:30:00 (want this answer)
$timeToEat = date("H:i:s",($timeToEat));//14:00:00 (want this answer)
$startEating = date("H:i:s",($startEating));//21:30:00  (want this answer)

3 个答案:

答案 0 :(得分:0)

00:30:00是午夜的一半,而不是30分钟。您可以使用strtotime-30 minutes功能减去30分钟。

echo date('H:i:s', strtotime('22:00:00 -30 minutes'));

其他间隔可以类似地完成:

echo date('H:i:s', strtotime('22:00:00 -1 hour -30 minutes'));

在您最近的例子中,您想要:

echo date('H:i:s', strtotime($lastMealRequested . ' -30 minutes')); 

因为您希望strtotime进行数学计算,而不是PHP(或不直接使用PHP)。

答案 1 :(得分:0)

正如其他人所指出的那样,00:00:30不是30分钟;现在是12点半(午夜)。

如果你记住strotime()返回一个Unix时间戳(即:自1970年1月1日00:00:00 UTC以来的秒数),你应该可以解决它:

<?php
function timeDiff( $t1, $t2, $echo = true ) {
    if ( $t2 > $t1 ) {
        $temp_time = $t2;
        $t2        = $t1;
        $t1        = $temp_time;
        unset( $temp_time );
    }

    $diff = $t1 - $t2;

    $hours   = floor( $diff / 3600 );
    $minutes = $diff % 3600 / 60;

    $timeFormatted = "$hours:$minutes:00";

    if ( $echo ) {
        echo $timeFormatted . '<br />';;
    }

    return strtotime( $timeFormatted );
}


$hourToEatLastMeal = strtotime( '00:30:00' );
$hourToEatWorkout  = strtotime( '01:30:00' );
$hourBetweenMeal   = strtotime( '07:30:00' );
$lastMealRequested = strtotime( '22:00:00' ); //22:00:00 //value getting from query.

$startEating   = timeDiff( $lastMealRequested, $hourToEatLastMeal );//21:30:00 (want this answer)
$timeToEat     = timeDiff( $startEating, $hourBetweenMeal ); //14:00:00 (want this answer)
$whenToWorkout = timeDiff( $timeToEat, $hourToEatWorkout ); //12:30:00 (want this answer)

但这远非理想。让strtotime()做数学更好。您可以explode()减去您的值以获得小时和分钟:

function timeDiff( $t1, $t2 ) {
    $timeArr = explode( ':', $t2 );

    return strtotime( $t1 . " - {$timeArr[0]} hour - {$timeArr[1]} minutes" );
}

echo date( "H:i:s", timeDiff( '22:00:00', '00:30:00' ) );

答案 2 :(得分:0)

又一种解决方案

$hourToEatLastMeal = '-30 minutes';
$hourToEatWorkout = '- 1 hour - 30 minutes';
$hourBetweenMeal = '- 7 hours - 30 minutes';

$lastMealRequested = new \DateTimeImmutable($row['lastMeal']);

$startEating = $lastMealRequested->modify($hourToEatLastMeal);
$timeToEat = $startEating->modify($hourBetweenMeal);
$whenToWorkout = $timeToEat->modify($hourToEatWorkout);

echo $whenToWorkout->format('H:i:s') . PHP_EOL;
echo $timeToEat->format('H:i:s') . PHP_EOL;
echo $startEating->format('H:i:s') . PHP_EOL;