What is a better way in the code below to add a new value to the variables $timestop and $time_diff if the condition is met?
//Calculates difference in time using 24h format
$timestart = strtotime("14:00:00");
$timestop = strtotime("07:00:00"); //if smaller value, it must end next day and meets the condition below
$time_diff = $timestop - $timestart; //elapsed time
if ($time_diff < 0 || $time_diff == 0) //if result is negative value, $timestop ends next day
{
$timestop = strtotime( '+1 day' , strtotime("07:00:00") ); //+ 1 day changes timestamp
}
/* UPDATED */
$time_diff = $timestop - $timestart; //added again
echo $time_diff;
No, overwriting variables is no problem, and it’s daily routine.
You are not even overwriting in any other type, you’re just resetting to another integer value.
It’s even better, because you don’t waste any extra memory space (which of course is not high, but think of it in a big scale).