# How to calculate two hours out of two?

I have twice as 10:00 am

and 7:00 pm

.

And from this I want to get the total number of hours. Since from now on I should get 9 hours.

How do I do it?

I blew it up with :

, but then subtracted 7 from 10 and returned the result 3, which is incorrect because it should return 9.

+3

source to share

<?php echo  strtotime('7:00 pm')-strtotime('10:00 am');?>

Get the difference between labels

EDIT

echo (strtotime ('07: 00 pm ') - strtotime ('10: 00 am')) / (60 * 60); // displays 9

60 * 60 = 1 Hrs

EDIT

\$fromTime = '3:00 pm';
\$toTime = '12:00 am';
\$timediff   =   (strtotime(\$toTime)-strtotime(\$fromTime))/(60*60);
echo \$timediff >= 0 ?  \$timediff : (24 + \$timediff);

+4

source

You can use PHP's strtotime function to convert it to unix time and so you can do further calculation with it.

+1

source

I assume you can get information about am

or pm

! With this information you can do if()

to make it correct!

if()

it's PM, add 12 hours, if it's not PM, do nothing.

Do this with the times you are comparing:

if (pm==1){
time+=12;
}

so it 10:00 am = false

will be 10:00 and it 7:00 pm = true

will be 19:00

19:00 - 10:00 = 9 hours = win

...

+1

source

<?php echo  date('H:i:s',strtotime('7:00 pm')-strtotime('10:00 am'));?>

+1

source

Use DateTime interface, its simple

\$day1= new DateTime("today 01:33:26am");
\$dayd2= new DateTime("today 10:40:36pm");         //Output: Hours: 21
\$interval= \$day1->diff(\$day2);
\$h =  (\$interval->days * 24) + \$interval->h;
echo "Hours: ".\$h

Here h = number of hours. \$ interval-> days means how many days are derived from the difference if we get one (1) day then add 1 * 24 (hours) = 24 + 21 = 46 hours, but here day1 and day2 contains today so that means 0 * 24 = 0 + 21 = 21

Final withdrawal: 21 hours

0

source

All Articles