Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.