-1

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.

Regards,

tarique
  • 2,201
  • 5
  • 19
  • 28

3 Answers3

1
$hours = (abs(strtotime($timestamp1)-strtotime($timestamp2)) / 60) / 60;
Geoffrey Wagner
  • 818
  • 1
  • 5
  • 11
1

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

Steve Nguyen
  • 5,854
  • 5
  • 21
  • 39
0

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);
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131