2

Im trying to solve this since two days without success... First of all my code:

PHP-Version: 5.4

SCRIPT1:

query = "SELECT start, end FROM timetable WHERE ........";
$result = mysql_query($query, $db) or die(mysql_error());
$sqlarr = mysql_fetch_array($result, MYSQL_ASSOC);
$times  = array($sqlarr['start'], $sqlarr['end']);

$calced = strtotime($times[1]) - strtotime($times[0]);
$total = date("H:i:s", $calced-3600);            //<-- -3600 Fixed it
echo "<br>Total: ".$total;

The start and end times are in format 00:00:00. Everytime this script calculates it appends 1 hour to the result. So if im going for a result of 5 minutes i´ll get 01:00:05. Why???

This one is even more strange. SCRIPT2:

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    echo " | ".$row['total']."<br>";
    $add += strtotime($row['total']);}

echo $add;

The first script calculates the total time from start to end. The second one should get the total-times out of the db and calculate the sum of all entries. For every entry the second script substracts 2 hours.

Example: Database => Start = 12:32:00 End = 12:32:15

Script1 Result1 = 01:00:15 (Where is this extra hour coming from?) FIXED

Every Result1 is stored in the same table(db). Script2 is loading all this rows and handling them by a while-loop.

According to how many entries there are the script subtracts serval hours.
0 Entries => Total: 00:00:00
1 Entry => Total: 23:00:xx
2 Entries => Total: 20:00:xx
3 Entries => Total: 18:00:xx
4 Entries => Total: 16:00:xx
So, with 2 entries it continues subtracting 2 hours from every total-calculation which isnt correct, abviously.

Thanks to you guys. Using DateTime made this simple and bugfree!

2
  • So I didn't really get it, 00:00:05 + 00:00:05 = 22:00:10, what is start and end in this example? So start = 00:00:10 and end = 22:00:10? Commented May 23, 2014 at 7:51
  • Ill edit my first post to get it a bit clearer. Commented May 23, 2014 at 7:58

2 Answers 2

2

I am not really sure of your case with using strtotime function, but I've tried this solution and it worked, if you have php > 5.2:

You can use the DateTime class and date_diff function to get the difference date, read more here: https://www.php.net/manual/en/datetime.diff.php

Example:

// start date
$start = new DateTime("09:23:38");
// end date
$end = new DateTime("09:23:54");

// calculate difference
$calc = date_diff($start, $end);

// prints 00:00:16
echo $calc->format('%h:%i:%s');

According to this answer It seems like by default date starts from 1:00:00 so you could subtract your date by 3600 seconds (1 hour) like this:

$start = strtotime("11:23:38");
$end = strtotime("11:23:54");

$calc = $end - $start;
echo date('H:i:s', $calc - 3600);
Sign up to request clarification or add additional context in comments.

3 Comments

Im a newbie with converting and calculating with dates and times. I tried this on a testpage. Worked for now. I get 0:0:x. Is it possible to get this into 00:00:xx?
Alright. -3600 fixed my first problem. There is now still the second one. The "total-times" are stored together in 1 table. The 2nd script loads all the entries and sumarizes them. For every entry in my table the end-result gets substraced by 2 hours. I hope you know what I mean.
If I get you correctly, that's because you subtract 2 time differences, so each time difference has +3600 extra. try doing totaltime - (3600 * amountOfDifferences).
0

The problem comes from :

 echo date('H:i:s', $calc);

$start - $end gives you the expected value in seconds. It depends on what value date('H:i:s', 0) is. On my computer, it's not midnight on 1 jan. 1970 ! This link as example : http://sandbox.onlinephpfunctions.com/code/81e56cbce16f6dacfedd2cb376b946a540bce36d.

You might consider substracting date('H:i:s', 0) from your result to get correct value.

Or using DateTime as @Ben Beri suggested you to do as it is (I think) the best way to do.

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.