-2

$departure is 2014-06-02 and $date is 2014-06-01. I actually ran this through a sandbox and it came out right. However, this is my output on the page: 2014-06-02 date: 2014-06-01 g: 16222

This is my code:

$days = 0;
$departure = strtotime($r['departure']);
echo $r['departure'];
echo " date: ".$date;
$datediff = abs(strtotime($date) - strtotime($departure));
$days = floor($datediff/(60*60*24));
echo " g: ".$days++;

So days should be 2 yet it is 16222. What is going on here?

Burning Hippo
  • 787
  • 1
  • 20
  • 47
  • Provide the code that we could run on our machines: substitute `$r["departure"]` and other undefined variables with constant values. PS: http://php.net/manual/en/datetime.diff.php – zerkms May 19 '14 at 00:40
  • you should convert them into timestamps, because the calculations are both from the year 1900 and can't fail if you use timestamps ^^ – Tosfera May 19 '14 at 00:41
  • PS: `echo " g: ".$days++;` - do you realize what postfix increment operator is for? – zerkms May 19 '14 at 00:41
  • @Tosfera: "and can't fail if you use timestamps" --- what about 1960 and 2100 year dates? – zerkms May 19 '14 at 00:41
  • @zerkms just make sure you always use 1900. – Tosfera May 19 '14 at 00:42
  • @Tosfera: what about 1901? What timestamp will it have? – zerkms May 19 '14 at 00:42
  • @Tosfera what do you mena "use timestamps"? – Burning Hippo May 19 '14 at 00:43
  • 1
    @user2690363: don't listen to them, just use `DateTime` class – zerkms May 19 '14 at 00:43
  • You can use the DateTime class indeed, but if you want to have your hands close to your own code you can use timestamps ( usefull if you want to go to an unsupported date / time ) – Tosfera May 19 '14 at 00:45
  • 1
    @Tosfera: "close to your own code" --- what does it even mean? What is the *technical reason* to transform a date from convenient format to a very limited format? – zerkms May 19 '14 at 00:46
  • @zerkms that's quite simple to answer; cross platforms and transfering your datetime to different languages without losing the real DateTime. Every language has another way of handeling their DateTime. – Tosfera May 19 '14 at 00:50
  • @Tosfera: "cross platforms and transfering your datetime to different languages without losing the real DateTime" -- that's right. `2014-06-02` is cross platform, timestamp is not. "Every language has another way of handeling their DateTime" --- that's correct, and majority support the explicit date type. So, is there a **REAL TECHNICAL** reason to transform it to seconds to calculate the time diff in php? – zerkms May 19 '14 at 00:53
  • 1
    possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Nanne May 19 '14 at 11:38

3 Answers3

2

It looks like you are converting $r["departure"] with strtotime twice. That is one too many. try your code with only one conversion and it should work better.

iewebguy
  • 316
  • 3
  • 16
1

I usually use a class Date, here are the functions that may help you (I've added an example in the end):

<?php

function sqlInt($date) {
    return mktime($date['hour'], $date['minutes'], 0, $date['month'], $date['day'], $date['year']);
}

function differenceInt($dateStart, $dateEnd) {
    $start = sqlInt($dateStart);
    $end = sqlInt($dateEnd);
    return $end - $start;
}

function difference($dateStart, $dateEnd) {
    $difference = differenceInt($dateStart, $dateEnd);
    $result = array();
    $result['hours'] = $difference/3600;
    $result['minutes'] = $difference/60;
    $result['days'] = $difference/86400;
    return $result;
}

$dateStart = array('hour'=>'0', 'minutes'=>'0', 'month'=>'6', 'day'=>'1', 'year'=>'2014');
$dateEnd = array('hour'=>'0', 'minutes'=>'0', 'month'=>'6', 'day'=>'2', 'year'=>'2014');

echo '<pre>';
print_r(difference($dateStart, $dateEnd));
echo '</pre>';

?>
Raul Leaño Martinet
  • 2,035
  • 6
  • 28
  • 44
0

its simple u need to use DateTime class

$date1 = new DateTime($firstdate);
$date1ready = $date1->format('Y-m-d');

$date2 = new DateTime($seconddate);
$date2ready = $date1->format('Y-m-d');

$difff = $date2->diff($date1);

$days = $difff->d;
manuel_m2
  • 45
  • 5