Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the difference between two time/dates using php?

Tags:

php

I am wanting to find out the time difference in minutes between two dates which is in the format d-m-Y H:i (14-04-2009 12:15) using php?

like image 937
jimbo Avatar asked Dec 17 '22 04:12

jimbo


1 Answers

Parse the times into timestamps using strtotime() and then simply subtract one from the other.

After that you can get the number of minutes, days and so on by using math functions.

For example:

// $date1 and $date2 are given
// the difference is in seconds
$difference = strtotime($date1) - strtotime($date2);

// getting the difference in minutes
$difference_in_minutes = $difference / 60;

Reference: strtotime()

like image 62
brainfck Avatar answered Jan 18 '23 16:01

brainfck