-2

I am using this code to calculate the time period. In fact, I want the period between two HH:MM:SS moment and have a result in HH:MM:SS format.

$time1 = strtotime('00:00:00');
$time2 = strtotime('00:00:07');
$diff = $time2 - $time1;
$diff = date('H:i:s', $diff);

I'm expecting 00:00:07 but I get 01:00:07. What could be the problem?

Irvin run here, and get right answer but I run the same code on my local machine and wrong!! Is it timezone or maybe some configuration effect the result?!

Farzad Salimi Jazi
  • 760
  • 10
  • 25
  • Everything is working fine with my demo. Exactly same code from you. The result is `00:00:07`. https://eval.in/684340 – Irvin Nov 24 '16 at 00:34
  • @Irvin, I run here on local machine and it give me 01:00:07, It crazy! but maybe it depend on timezone !!?? – Farzad Salimi Jazi Nov 24 '16 at 00:36
  • Yeah it might be. – Irvin Nov 24 '16 at 00:37
  • 1
    Yes, it's probably a timezone issue. `$diff` is a timestamp in UTC, but `date()` produces the output in the current timezone. You're presumably 1 hour behind UTC. – Barmar Nov 24 '16 at 00:44
  • 1
    simple fix: `date_default_timezone_set('UTC');` – Funk Forty Niner Nov 24 '16 at 01:08
  • Possible duplicate of [PHP date(); with timezone?](http://stackoverflow.com/questions/20288789/php-date-with-timezone) – Funk Forty Niner Nov 24 '16 at 01:08
  • @Fred-ii- , if I set the time zone as UTC , is it temporary! I mean for other functions that use time , it still would be UTC? – Farzad Salimi Jazi Nov 24 '16 at 01:43
  • what other functions? and here, this Q&A will most likely explain it http://stackoverflow.com/questions/7587435/setting-timezone-to-utc-0-in-php mostly the most upvoted/accepted answer http://stackoverflow.com/a/7587637/1415724 - it's server dependant and on its location. If you want a user's time/date and if from another location/country etc, then you need to use a JS solution. – Funk Forty Niner Nov 24 '16 at 01:48
  • Even though it might work in many cases, this is still a gross misuse of date functions. – Sammitch Nov 24 '16 at 01:53
  • @Sammitch , As you see this is a question not answer ! If you know a way I appreciate your help! – Farzad Salimi Jazi Nov 24 '16 at 06:12

3 Answers3

0

It's because when you minus time 1 from time 2 you end up with 7;

The date function needs a timestamp, 7 is not a timestamp.

You could use http://php.net/manual/en/datetime.diff.php to calculate the difference instead?

Antony Thompson
  • 1,608
  • 1
  • 13
  • 22
0

Please stop using strtotime and date functions. Use the DateTime class.

And if you would have searched better, finding difference for two dates has already been all over SO. For instance, my answer here. Slightly changed it would look like:

$create_time = "00:00:00";
$current_time="00:00:07";

$dtCurrent = DateTime::createFromFormat('H:i:s', $current_time);
$dtCreate = DateTime::createFromFormat('H:i:s', $create_time);
$diff = $dtCurrent->diff($dtCreate);

echo $diff->format("%H:%I:%S"); // to get HH:MM:SS format

This returns 00:00:07 See DateInterval::format for more formatting details.

Community
  • 1
  • 1
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • @Nordenheim, I would accept the answer that solve it , Your answer is good but sometime the times are not right format and it stop my code, I don't want it to happen, In my previous method, even if they are not right format like HH:MM:SS it won't give me internal error. Do you know how to ignore object errors in the case it failed to create object ! – Farzad Salimi Jazi Nov 24 '16 at 01:24
  • @FarzadSalimiJazi there are a lot of possible answers starting with `error_reporting` setting and ending with poorly designed and implemented architecture that you might have. It all depends on the code you have and might require major refactoring. To get advice on the latter there is [Code Review](http://codereview.stackexchange.com/) – Kevin Kopf Nov 24 '16 at 01:41
  • Everyone: I have just cleaned up a lot of clutter in the comments. The comments are not for extended discussion. Please do not use them to start or finish wars about whether OOP is amazing, a failed experiment, or whatever. Thank you. – elixenide Nov 24 '16 at 02:18
0

You should consider the UNIX epoch which cancels timezone diffrences in ini settings, essentially. it's Jan 1st '70. You can check your server/app's setup for timezone by adding e, O or P. Check this code:

$time1 = strtotime('00:00:00 +0000');
$time2 = strtotime('00:00:07 +0000');
$epoch = strtotime('01 Jan 1970');
$diff = $time2 - $time1;
var_dump($time1,$time2,$epoch,$diff);
$diff = date('H:i:s', $epoch+$diff);
echo $diff;
Tala
  • 909
  • 10
  • 29