开发者

how to calculate exact hours from two timestamp in php?

I need to get the difference of hours from between two timestamps. Can you tell me how can it be done using php?

Thanks in advance.

R开发者_开发百科egards,


$hours = (abs(strtotime($timestamp1)-strtotime($timestamp2)) / 60) / 60;


You can feed the values of timestamps into strtotime(), this will get you the UNIX TIME STAMP in seconds since 1970. It'll be two large integer second values. So then do a subtraction between the two values and convert that into whatever you're looking to get. e.g.

minutes, hours, days, weeks, etc... by doing normal *60, *60, *24, *7 computation


Use strtotime to convert the timestamps into seconds, subtract them, use abs to get the absolute value (no negative numbers here!), divide by 3600 seconds (1 hour), and then round to the precision you'd like.

$difference = round(abs(strtotime($stamp_one) - strtotime($stamp_two)) / 3600, 2);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜