I have following php function, that creates an array of all the days between a start date and an end date:
function dateRange( $first, $last, $step = '+1 day', $format = 'Y-m-d' ) {
$dates = array();
$current = strtotime( $first );
$last = strtotime( $last );
while( $current <= $last ) {
$dates[] = date( $format, $current );
$current = strtotime( $step, $current );
}
return $dates;
}
I then use:
if (in_array($this_date,dateRange($start_date, $end_date))) {... do sth ...} else {... do sth else...}
Now I think to best implement this in a while-loop when reading the dates from the database. There are several date periods in the database and I would like to create an array with all of them when reading the rows, and finally check if in_array($this_array_contains_all_date_ranges_from_db).
If I try to print $array after using dateRange($start_date,$end_date), it returns only an empty “Array”. However, if I use the above mentioned if, it works fine. Also, I tried e.g. $this_date_range=dateRange($start_date,$end_date); but it was empty as well… I am not so experienced with functions, so I just could not figure out how can I create a var that will contain all the date ranges?
Thank you very much in advance!! 🙂
When you say “print $array”, are you doing a literal
print($array)? GettingArrayas the output from that is expected – you cannotprint()an array. You have to useprint_r()orvar_dump(). Both will dump data structures into a textual format.