-1

My question is: I have two dates in string format:

string(10) "2016-21-10"
string(10) "2016-05-10"

I can not figure out how to compare them. So basically i need the difference between the two given date. How can I achieve it?

Alok Patel
  • 7,842
  • 5
  • 31
  • 47
  • 1
    convert the dates into the timestamp and then compare easily. If you need difference then Check [this](http://stackoverflow.com/questions/39869526/get-number-of-days-from-given-dates-in-a-array-php/39869674#39869674) for the difference only. – Murad Hasan Oct 05 '16 at 10:08
  • Please do a search before asking a question. Google has SO very well indexed, you are likely to solve most of your issues with just a search – RiggsFolly Oct 05 '16 at 10:22

1 Answers1

1

You can use createFromFormat() function to create DateTime object from string.

Create two DateTime object like this,

$date1 = DateTime::createFromFormat('Y-d-m', '2016-21-10');
$date2 = DateTime::createFromFormat('Y-d-m', '2016-05-10');

To get the difference between two dates you can use diff() function. Use it like this,

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

You can use the $interval variable to get the difference between two dates.

Alok Patel
  • 7,842
  • 5
  • 31
  • 47