0

I have a couple of columns in my mysql table of datatype time. The times are listed in military hours I believe, and i need get the difference(hours/mins) between the two times in PHP. Example:

MYSQL:

Start_time  End_time
08:00:00    09:15:00

In php how can calculate (Start_time) - (End_time) = (result should be in mins).

dave
  • 14,991
  • 26
  • 76
  • 110

1 Answers1

1

You can get it in mysql using this.

SELECT TIMESTAMPDIFF(SECOND, column1, column2);

to get it in php see this answer

Getting time difference between two times in PHP

Or use something like the following where you would use the dates you got from mysql as the strings to DateTime.

1 <?php
2   
3 $time1 = new DateTime('13:00:59');
4 $time2 = new DateTime('19:01:00');
5 $interval = $time1->diff($time2);
6 print_r($interval->format('%h:%s'));

To get the interval in a different format change the format string. You can find the valid string values in the DateTime documentation here

To get minutes use "%i" or "%l", the latter has leading zeros.

1 <?php
2   
3 $time1 = new DateTime('13:00:59');
4 $time2 = new DateTime('19:01:00');
5 $interval = $time1->diff($time2);
6 print_r($interval->format('%l'));
Harry
  • 11,298
  • 1
  • 29
  • 43