tags:

views:

30

answers:

2

I am trying to substract 24 hours time format and then convert it into minutes but it does not work well.

Here is my code

$time1 = '2010-08-05 23:00:00';
$time2 = '2010-08-05 00:00:00';

echo round( (strtotime($time2) - strtotime($time1)) / 60);

it will display this -1380.

if you put 1-23 hour in time2 it will work. I tried to convert time in 12 hours but it didn't work.

Any help would be greatly appreciated.

The result is in minutes not in hours.

A: 

I think this is what you are trying to get:

echo round( (strtotime($time1) - strtotime($time2)) / 3600);

Firstly you were subtracting the times the wrong way round, then you were only dividing by 60 which was giving you your answer in minutes.

sixfoottallrabbit
+2  A: 

strtotime() returns a result in seconds (in the Unix timestamp format). You will need to divide by 3600 to convert to hours.

Try this:

$time1 = '2010-08-05 23:00:00';
$time2 = '2010-08-05 00:00:00';

echo abs ( round( (strtotime($time2) - strtotime($time1)) / 3600) );

The abs() function returns an absolute value. In this case, the final result is 23 hours.

peterjmag
`abs()` is the way to go - this way it doesn't matter which timestamp is the earlier one.
Pekka