0

I have done a javascript code and it works good. This script gets the difference between two dates. Now I want to write this script in php language, but it returns wrong values.

JSFiddle (js version)

I want to achieve this via php. This is what I have done so far in PHP:

$NOW = strtotime("now");
$Time = 151048737944;

function dateDiff($date1, $date2)
{

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

    if (floor($diff / 31536000000)) {
        echo floor($diff / 31536000000) . " year";
    } else if (floor($diff / 86400000)) {
        echo floor($diff / 2592000000) . " months";
    } else if (floor($diff / 86400000)) {
        echo floor($diff / 86400000) . " days";
    } else if (floor($diff / 3600000)) {
        echo floor($diff / 3600000) . " hours";
    } else if (floor($diff / 60000)) {
        echo floor($diff / 60000) . " minutes";
    } else {
        echo "now";
    }

}

dateDiff($NOW, $Time);

For 151048737944 it return: 4 year

1stthomas
  • 731
  • 2
  • 15
  • 22

2 Answers2

0

Are you sure your timestamp $Time is correct? it prints the following date 1992-12-214 when used with date("y-m-d") can you make sure that it is correct?

Moreover, you should use date_diff in php rather than converting this javascript see here for manual manual date_diff or use this stackoverflow post to calculate the difference stackoverflow

see below updated code for your solution you are using wrong since time figures see below for the correct ones

<?php 
$NOW = strtotime("now");
$date = new DateTime('2017-11-01');

$Time = $date->getTimestamp();

function dateDiff($date1, $date2){
   $diff = abs($date1 - $date2);

  if (floor($diff / 31536000 )) {
    echo floor($diff / 31536000) . " year";
  } else if (floor($diff / 2592000 )) {
    echo floor($diff / 2592000 ) . " months";
  } else if (floor($diff / 86400  )) {
    echo floor($diff / 86400  ) . " days";
  } else if (floor($diff / 3600 )) {
    echo floor($diff / 3600 ) . " hours";
  } else if (floor($diff / 60 )) {
    echo floor($diff / 60 ) . " minutes";
  } else {
    echo "now";
  }

}
echo "difference between ".date('Y-m-d',$NOW)." and ".date("Y-m-d",$Time).'is <br /> ';
dateDiff($NOW, $Time);

?>
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
  • This should be a comment, consider adding an example to your answer :) – Ofir Baruch Nov 12 '17 at 12:03
  • yeah but i recently received an email from moderator that i should not copy paste solution from other sites or stackoverflow but i should provide proper link to the actual solution if i am not providing custom solution. :( should i delete the post ? – Muhammad Omer Aslam Nov 12 '17 at 12:07
  • 1
    You should address the error first and try to answer why does OP get the wrong date. Afterwards, you might suggest the date_diff function as a better way to address this case (and it's also a better practice as a programmer). In the suggestion you should use, for instance, the example code from the manual while relating to the OP code. (Just edit your answer, no need to remove it) – Ofir Baruch Nov 12 '17 at 12:09
  • i updated the answer the above should print 11 days difference for you – Muhammad Omer Aslam Nov 12 '17 at 12:39
0

Use DateTime diff function:

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2015-10-13');
$interval = $datetime1->diff($datetime2);
$yearsdifference = $interval->format('%y'); 

From the docs.

  • got error `Call to a member function diff() on integer` –  Nov 12 '17 at 12:13
  • Try again, I promise you I'm getting this: `string '6' (length=1)` when I `var_dump($yearsdifference);` – Luis Cabrero Nov 12 '17 at 12:21
  • Make sure ure calling right the construct of DateTime [docs](http://php.net/manual/en/datetime.construct.php) right, you need to pass string to instanciate new DateTime object – Luis Cabrero Nov 12 '17 at 12:25
  • It works with this format `2009-10-11` but i using `timestamp` –  Nov 12 '17 at 12:26
  • 1
    Convert [timestamp to DateTime](http://php.net/manual/en/datetime.settimestamp.php) following the first example :) – Luis Cabrero Nov 12 '17 at 12:30