0

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;
19
  • 1
    If the variable didn't update, the condition in the if statement was not met. Commented Jun 17, 2012 at 0:11
  • 2
    Okay, so if you add one day, the time is the same. Any time + 24 hrs is the same time. Commented Jun 17, 2012 at 0:12
  • 1
    before the if: 1339909200, after the if: 1339995600. of course it does update. Commented Jun 17, 2012 at 0:13
  • 2
    Why don't you use if ($time_diff <= 0) instead of if ($time_diff < 0 || $time_diff == 0) ? Commented Jun 17, 2012 at 0:21
  • 1
    Try using the DateTime classes. New fashioned way :) Commented Jun 17, 2012 at 0:25

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.