0

i m tried hard to get the date in the date formate after subtracting two dates but not getting the correct result ,and the result is 1970-01-07 05:00:00 but i require the result 2013-02-07 12:02:00

$start_date =strtotime('2013-02-13 12:02:00');
$end_date =strtotime('2013-02-20 12:02:00');
$total = ($start_date - $end_date);
echo date("Y-m-d H:i:s",$total);
Kara
  • 6,115
  • 16
  • 50
  • 57
Shahbaz
  • 3,433
  • 1
  • 26
  • 43

3 Answers3

2

As of PHP 5.3 you can use the DateTime::diff() method together with the DateInterval class for that:

<?php

$start = new DateTime('2013-02-13 12:02:00');
$end   = new DateTime('2013-02-20 12:02:00');

$interval = $end->diff($start);
echo $interval->format('%Y-%M-%D %H:%I:%S');

In addition, it is the supposed way to do as of PHP5.3, as it handle timezones, daylight saving times (leap hours) and leap years properly.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
2

you are getting the correct result as after subtracting two date result is 7 days which is added to default date 1970-01-01

simply-put
  • 1,068
  • 1
  • 11
  • 20
0

Try the code below

$date2 ='2013-02-13 12:02:00';
$date1 ='2013-02-20 12:02:00';

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);
Techie
  • 44,706
  • 42
  • 157
  • 243