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?
Asked
Active
Viewed 7,527 times
2 Answers
12
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()

NullUserException
- 83,810
- 28
- 209
- 234

brainfck
- 9,286
- 7
- 28
- 29
-
1PHP may assume your date is in m-d-Y format when the numbers for month and day make the format ambiguous (the example you give is unambiguous, as there are not 14 months). You may need to parse and swap the day and month. – Lucas Oman Nov 20 '09 at 14:08
-
Of course, I didn't recognize that the given date is not in standard US-english format. – brainfck Nov 20 '09 at 14:25
-
Exactly. strtotime might give you unexpected results since the date format you're using is unusual. – Salman A Nov 20 '09 at 14:26
-
`strtotime()` assumes European / Australian format (dd-mm-yyyy) when the separators are hyphens. Forward-slashes make it use US format (mm-dd-yyyy) – Phil Dec 06 '11 at 01:05
0
date_default_timezone_set('Asia/Kolkata');
$currentDateTime = date('m/d/Y H:i:s');
$model_current_time = date('Y-m-d H:i:s',
strtotime($currentDateTime));
echo $model_current_time."------";
$date = DateTime::createFromFormat('d/m/Y h:i:s A',
$row['model_creation_time']);//get from resouses
$new_date_format = $date->format('m/d/Y H:i:s');
$model_creation_time = date('Y-m-d H:i:s',
strtotime($new_date_format));
echo $model_creation_time;
$datetime1 = new DateTime($model_current_time);
$datetime2 = new DateTime($model_creation_time);
$interval = $datetime1->diff($datetime2);
echo $interval->d;
echo $interval->h;
echo $interval->s;

Manthan Patel
- 1,784
- 19
- 23