Hi all, this is an interesting one.
I have two dates, I have today and I have an adjusted date. Today is obviously todays date, and adjusted date is X weeks from today.
The info resides in an array like so:
$date_array[today] and $date_array[adjusted]
What I want to know, is how can I do an automagic that finds out all the dates inbetween those two dates?
For example, consider the two dates to be 2005-11-22 and 2005-11-15 how can I get a script to take one and fill in the blanks to create X new dates (in another array say) that are all legit?
It would be perfect if it would create this:
2005-11-22
2005-11-21
2005-11-20
2005-11-19
2005-11-18
2005-11-17
2005-11-16
2005-11-15
or similar.
It's a toughy for me, I'm really not this advanced... (but I'm guessing it will be something easy, knowing the cool functions PHP seems to have).
Rob
auto find out the dates between two other dates
Moderator: General Moderators
If there's something in the function below that you don't understand, just let me know.
Code: Select all
function dateRange($firstDate, $secondDate)
{
//
// define the required variables
$dateList = array();
$outOfRange = false;
$currentDate = $firstDate;
//
// convert the dates into timestamps
$firstStamp = strtotime($firstDate);
$secondStamp = strtotime($secondDate);
//
// determine which way to go (in time)
$direction = ( $firstStamp < $secondStamp )
? 'tomorrow' : 'yesterday';
//
// generate the date list
while( !$outOfRange )
{
$dateList[] = $currentDate;
$currentDate = date('Y-m-d', strtotime( "$currentDate $direction" ));
$outOfRange = ( end($dateList) == $secondDate );
}
//
// return the generated list
return $dateList;
}- robster
- Forum Contributor
- Posts: 360
- Joined: Wed Jul 16, 2003 8:28 am
- Location: Sunshine Coast, Australia
wow, it's perfect! Thank you 
There is something I've never seen before though, this code here:
Could somebody please explain what's happening here?
Thanks again,
Rob
There is something I've never seen before though, this code here:
Code: Select all
$direction = ( $firstStamp < $secondStamp )
? 'tomorrow' : 'yesterday';Thanks again,
Rob
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
its a comparison operator called ternary.. basically a condensed if statement