Kaeder
Kaeder

Reputation: 5

How to manipulate php time data and get a result in hour:minute format

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

Upvotes: 0

Views: 129

Answers (2)

Saroj
Saroj

Reputation: 1373

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;

Upvotes: 0

Sahathulla
Sahathulla

Reputation: 324

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");

Upvotes: 2

Related Questions