-1

Please try to understand the problem first.

I want PHP code to find the difference between two dates (number of days) in php and store the result as numeric/int value in a variable in php

I know date_diff() function will be used. But How ?? I'm using wampserver 2.2 on Windows 7 My PHP version is 5.3.12 Please help

3 Answers3

2
$date = new DateTime('2014-01-31');

$date2 = new DateTime('2014-02-04');

$diff = date_diff($date, $date2, true);
$difference = $diff->format("%a"); // 4

This will give you the difference in days. If you want something else than days you can see the possible formats here: http://php.net/manual/en/dateinterval.format.php

As you can see, the function date_diff() is very easy to use. It will return you an DateInterval object, which you can then format to give you the desired result.

Philipp
  • 2,787
  • 2
  • 25
  • 27
  • I cold not understand the statement var_dump($diff->format("%a")); Actually I need the number of days between two dates to be assigned to a numeric variable.so that I can use tha variable later in the script – Rocky Alex Jul 30 '16 at 18:48
  • @RockyAlex It was just to display the value which is returned by `$diff->format("%a")`. If you take the snippet as it is now `$difference` will have the value 4 and you can work with it like with any other variable – Philipp Jul 30 '16 at 18:52
  • @RockyAlex So is there a problem/error with this code? – Philipp Jul 30 '16 at 19:47
  • 1
    Thank u it worked ultimately. Actually I was having some other logical errors which I fixed today. – Rocky Alex Jul 31 '16 at 08:14
-1

The best solution that I can think of is converting the date to timestampand then getting the difference. An example is :-

$date1 = date('Y-m-d H:i:s');
$date2 = date('Y-m-d H:i:s'); //I know it's same, it will be different in your case.
//conver it to timestamp now
$date1_timestamp = strtotime($date1);
$date2_timestamp = strtotime($date2);
//calculate difference.
$difference = $date2_timestamp - $date2_timestamp;
//the difference variable holds an int value.
//however, if you want to convert it back to readable date, you can do it like this
$difference_readable_date = date('m/d/Y', $difference);
Akshay
  • 2,244
  • 3
  • 15
  • 34
-1

my function for all the odds one or 2 times , date or timestamp..

 function time_Diff($time2_, $time1_ = false) {
        $date1 = new \DateTime();
        if ($time1_) {
            $time1 = preg_match('/[-\/]/', $time1_) ? strtotime($time1_) : $time1_;
            $date1->setTimestamp($time1);
        }
        $date2 = new \DateTime();
        $time2 = preg_match('/[-\/]/', $time2_) ? strtotime($time2_) : $time2_;
        $date2->setTimestamp($time2);
        $interval = $date1->diff($date2);
        $diffrent_ = $interval->format('%Y Years %m Months %d Days %H:%I:%S');
        $diffrent = str_replace(['00 Years', ' 0 Months', ' 0 Days', '00:00:00'], '', $diffrent_);
        $returner = str_replace(['01 Years', ' 1 Months', ' 1 Days'], ['01 Year', ' 1 Month', ' 1 Day'], $diffrent);
        return trim($returner);
    }