PHP - Find the difference between two times -


I try to calculate the difference of time between two times inputted through a time-pecker (in hours) I've done a javascript code, but to calculate it, the server side code has to be used because it is very important if you want me to post the JS code that works, let me know in the comments.

The difference between time is quite easy to calculate, but I need output in a specific format, for example, in the inputs of '07: 30 'and '14: 00' instead of 6.3, return 6.5 Will. The reason for this is that to calculate this time making use of the difference is easy for me.

I have tried PHP code:

Effort # 1:

  & lt ;? Php $ start_time = New date time ('07: 30 '); $ End_time = New Date Time ('14: 00 '); $ Time_diff = date_diff ($ start_time, $ end_time); Echo $ time_diff-> Format ('% h.% I'); ? & Gt;   

Expected 6.3 returns.

Effort # 2:

  & lt ;? Php $ start_time = "07:30"; $ End_time = "14:00"; $ Start_time = str_replace (":", "", $ start_time); $ End_time = str_replace (":", "", $ end_time); $ Res = $ end_time - $ start_time; $ Result = $ res / 100; Return results $; ? & Gt;   

Returns 6.7.

/ P>

  & lt ;? Php $ start_time = New date time ('07: 30 '); $ End_time = new date time ('14: 00 '); $ Time_diff = date_diff ($ start_time, $ end_time); $ Hour = (int) $ time_diff- & gt; Format ('% h'); $ Hour_part = ((int) $ time_diff-> format ('% i')) / 60; Hourly $ hour + $ hour_part; ? & Gt;   

Be sure to change the type to (int) before any calculation.
If you divide minutes into 60 seconds, then what part of an hour you will get to represent.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -