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

I have done almost everything I found on the internet. I have been trying for the past two days but couldn't get the desire answer. I just want to subtract two times. example 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)

00:30:00 is half past midnight, not 30 minutes. You can subtract 30 minutes with the strtotime function by using -30 minutes.

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

Other intervals can be done similarly:

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

In your recent example you'd want:

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

because you want the strtotime to do the math, not PHP (or not PHP directly).

As others have pointed out 00:00:30 isn't 30 minutes; it's half past 12 (midnight).

If you keep in mind that strotime() returns a Unix timestamp (i.e.: the number of seconds since January 1 1970 00:00:00 UTC), you should be able to work it out:

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

This is far from ideal, though. It's better to have strtotime() do the math. You could explode() the value you're substracting to get the hours and minutes:

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' ) );

Yet another solution

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