0

I am trying to create a program where it calculates start hour and finish hour and gets the time in between to see how many hours have been worked (the start and stop variables are replaced by time values from a database in the real code)

Here is an example:

$start = "11:15:35"
$stop = "17:30:13"


$start2 = date( 'G:i' ,strtotime($start) );
$stop2 = date( 'G:i' ,strtotime($stop) );

echo $stop2 ."<br>". $start2;
echo "<br><br>";
echo $stop2 - $start2;

I need the code to output something like 6.25 or 6h and 15min, currently it is only doing hours and outputting just a 6.

i have tried $stop2[1] & start2[1]

and changing around the 'G:i' bit with various other letters from http://php.net/manual/en/function.date.php

Sorry if this is a simple question, any answers appreciated!

Edit: misspelt minute

2
  • 1
    What if this wraps over midnight? Times without dates are pretty difficult to deal with. Remember date is dealing with date/datetime values, not arbitrary time intervals. Commented Jul 7, 2017 at 6:35
  • Sorry I forgot to mention, I have a date column in the MySQL table and probably going to add functionality to allow going over midnight in future or some way to deal with it Commented Jul 7, 2017 at 6:39

2 Answers 2

2

Simple way to get the difference between time as follows

    $a = new DateTime('11:15:35');
    $b = new DateTime('17:30:13');
    $interval = $a->diff($b);

    echo $interval->format("%H:%i:%S");

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

Comments

0

Try this

$hour_one = "17:30:13";
$hour_two = "11:15:35";

$h =  strtotime($hour_one);
$h2 = strtotime($hour_two);

$minute = date("i", $h2);
$second = date("s", $h2);
$hour = date("H", $h2);

$convert = strtotime("-$minute minutes", $h);
$convert = strtotime("-$second seconds", $convert);
$convert = strtotime("-$hour hours", $convert);
$new_time = date('H:i:s', $convert);

echo $new_time;

1 Comment

This answer is missing its education explanation.

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.