-3

Case One:

$start_date = 01-Mar-15
$end_date = 31-Apr-15

Fetch number of days for start-end date. Here it will be 31 days for both the dates.

Case One:

$start_date = 16-Mar-15
$end_date = 15-Apr-15

Here it will not be the same so fetch number of days for start_date i.e. 30 days.

How can I execute below condition based on above use-cases:

if($start_date_month_days == $end_date_month_days){
//do something with 31 dates
}else{
//do something with number of days for $start_date month
}

P.S. : I am not looking for number of days between two dates. But to fetch the number of days for specific start-end dates. That dates cold be in same/different months

Slimshadddyyy
  • 4,085
  • 5
  • 59
  • 121
  • Please be more clear. Are you looking for how many days are in between those two dates? – ksbg Apr 22 '15 at 08:50
  • Are you okay with using PHP's built-in [DateTime](https://php.net/manual/en/class.datetime.php) or the [Carbon](https://github.com/briannesbitt/Carbon) extension? – padarom Apr 22 '15 at 08:51
  • @treegarden, I am not looking for number of days between two dates. Just to fetch the number of days for specific `start-end` dates. That dates cold be in same/different months – Slimshadddyyy Apr 22 '15 at 08:52
  • There are tons of similar questions to this one asked in SO already, so it's worth doing a search. You'll find the PHP's built in `DateInterval` useful: http://php.net/manual/en/class.dateinterval.php Edit: I wasn't the downvoter btw – Darragh Enright Apr 22 '15 at 08:53
  • 3
    *"fetch the number of days for specific start-end dates"* – I have no idea what that means. I don't see how March 1st, April 31st results in 31 days. – deceze Apr 22 '15 at 08:53
  • That means if the start-end dates are, say for, month of March then it should return 31 days else if they are for apr, it should return 30 days – Slimshadddyyy Apr 22 '15 at 08:55
  • possible duplicate of [finding-the-number-of-days-between-two-dates](http://stackoverflow.com/questions/2040560/finding-the-number-of-days-between-two-dates) – Narendrasingh Sisodia Apr 22 '15 at 08:57
  • Sooooooo... you just want to figure out how many days there are in a particular month...? – deceze Apr 22 '15 at 08:57
  • Yes, but that will be based on months for start-end dates. Month could vary. Thus I explained with a user-case in my question – Slimshadddyyy Apr 22 '15 at 08:58
  • Sooooooo... You figure out how many days there are in your start date's month and then how many days there are in your end date's month and then you do something with it...? – deceze Apr 22 '15 at 08:59
  • Yes you are correct. – Slimshadddyyy Apr 22 '15 at 09:01
  • And existing questions like this didn't help...?! http://stackoverflow.com/questions/1686724/how-to-find-the-last-day-of-the-month-from-date – deceze Apr 22 '15 at 09:07

1 Answers1

-1
  $start_date = "01-Mar-15";
    $end_date = "31-Apr-15";

    $date1 = new DateTime(date("Y-m-d",strtotime($start_date)));
    $date2 = new DateTime(date("Y-m-d",strtotime($end_date)));
    $interval = $date1->diff($date2);

    echo "".$interval->d." days";

if($interval->d=="31"){
//do something
}
else{
}

Try this code

Vikas Umrao
  • 2,800
  • 1
  • 15
  • 23