Jump to content

Custom range question


k0z

Recommended Posts

If I have two dates, e.g.:

 

June 24th, July 2nd

 

and i want to range between the values, would would be the easiest way to accomplish this? Obviously

 

range(24,2) would not return the correct array.

 

The correct values SHOULD be:

 

array(24,25,26,27,28,29,30,1,2);

 

Because different months have different lengths, this could pose an issue. E.g.:

 

Feb 27, March 2nd should return:

 

array(27,28,1,2);

 

What would the easiest way to accomplish this be?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/206353-custom-range-question/
Share on other sites

This gives the actual dates in that range, you can modify it as needed to get just the day number -

<?php
$start = 'June 24th 2010';
$end = 'July 2nd 2010';

$current_date = date('Y-m-d',strtotime($start));
$end_date = date('Y-m-d',strtotime($end));
$array = array(); // an array for the dates
while($current_date <= $end_date){
$array[] = $current_date;
$current_date = date('Y-m-d',strtotime($current_date . '+ 1 day'));
}
echo "<pre>",print_r($array,true),"</pre>";
?>

This gives the actual dates in that range, you can modify it as needed to get just the day number -

<?php
$start = 'June 24th 2010';
$end = 'July 2nd 2010';

$current_date = date('Y-m-d',strtotime($start));
$end_date = date('Y-m-d',strtotime($end));
$array = array(); // an array for the dates
while($current_date <= $end_date){
$array[] = $current_date;
$current_date = date('Y-m-d',strtotime($current_date . '+ 1 day'));
}
echo "<pre>",print_r($array,true),"</pre>";
?>

 

Perfect! changed to:

 

$start = 'June 24th 2010';
$end = 'July 2nd 2010';
$current_date = date('Y-m-d',strtotime($start));
$end_date = date('Y-m-d',strtotime($end));
$array = array(); // an array for the dates
while($current_date <= $end_date){
   $array[] = date('j',strtotime($current_date));
   $current_date = date('Y-m-d',strtotime($current_date . '+ 1 day'));
}
echo "<pre>",print_r($array,true),"</pre>";

 

And working nicely.

 

Thanks for your help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.