You can create two DateTime
objects, one with the fixed time 07:45
and one with the current time.
Then you use diff
to calculate the difference between the two objects, making calculations about the different groups of differences in the object that will throw you diff
.
For example:
$dateStart = new DateTime("07:45:00");
$dateEnd = new DateTime();
$dateInterval = $dateEnd->diff($dateStart);
$strResult = sprintf(
'%d:%02d:%02d', //formato de salida
($dateInterval->d * 24) + $dateInterval->h, //horas
$dateInterval->i, //minutos
$dateInterval->s //segundos
);
echo "El evento empezará en $strResult";
Using diff
is the best way to calculate differences between dates, because it throws you a complete object with all kinds of differences. To give you an idea, this is all the information that will be in the object $dateInterval
of the code above. It will let you know, if necessary, not only how many hours / minutes / seconds there are between two dates, but also how many weeks, months, years ... and more information.
object(DateInterval)#3 (15) {
["y"]=>
int(0)
["m"]=>
int(0)
["d"]=>
int(0)
["h"]=>
int(3)
["i"]=>
int(5)
["s"]=>
int(50)
["weekday"]=>
int(0)
["weekday_behavior"]=>
int(0)
["first_last_day_of"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
int(0)
["special_type"]=>
int(0)
["special_amount"]=>
int(0)
["have_weekday_relative"]=>
int(0)
["have_special_relative"]=>
int(0)
}