Some adjustments must be made though…I must add some members to this array.
I must get back an array like this… array("title"=>"closed","day"=>"saturday","date"=>$date->format('l Y-m-d') ,"time"=> "12:30:00");
The app I am working on deals with arrays like the above…so I must add 3 more members to it…
I have not found a way to do it with array_map. or array_push…I have tried various syntax without success.
The example above hard codes the time to be 12:30:00 which may not be what you want if different days have different closing times. For example Sundays closed at 13:30:00. A quick example below show one possible way of solving it.
Sorry you are just confusing me now. $dates is an array that contains 1 or more DateTime objects. array_map is always going to return an array with the same number of items as $dates. What do you get if you print_r($dates)` in my last example?
$dateRange = dateRange(date('Y-m-d'), '2016-03-06');//just an example date range
$dates=array_filter($dateRange, dateFilter(['Tuesday']));//get the tuesdays within the above time segment
$closedDates = array_map(function ($date) {
return ['title' => 'Closed', 'day' => $date->format('l'),'date' => $date->format('l Y-m-d'),'time' => '12:30:00'];
}, $dates);
var_dump($dates);
//what var_dump prints...I did not use print_r...just for my own convenience
array(1) {
[1]=>
object(DateTime)#7 (3) {
["date"]=>
string(19) "2016-03-01 00:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/Athens"
}
}
At the moment your example will only contain a single item in the $dates array as there is only one Tuesday between today and the 6th March. In that case don’t bother with array_map. There are two ways to get what you want. Choose which ever you are happy with. Note that both these examples will only work if $dates contains a single item. They won’t work with more than one date.
The second just uses the fact that $dates contains a single item.
$dateRange = dateRange(date('Y-m-d'), '2016-03-06');
$dates = array_filter($dateRange, dateFilter(['Tuesday']));
/**
* This is no longer an option as we need to remove the holes in the array keys
* so that we can do $dates[0].
*/
$dates = array_values($dates);
$closed = [
'title' => 'Closed',
'day' => $dates[0]->format('l'),
'date' => $dates[0]->format('l Y-m-d'),
'time' => '12:30:00'
];
print_r($closed);
/**
Array
(
[title] => Closed
[day] => Tuesday
[date] => Tuesday 2016-03-01
[time] => 12:30:00
)
*/
If $dates contains multiple items then you are going to have to use my earlier examples with array_map
yes your code does the job…the only thing is that I am trying to figure out how to manipulate $format with in the function such as $result gives this back:
I manage to solve my problem with this(see the line where I placed the comment:solution):
function getDays($start , $iDays, $aDays,$format_d,$format_date,$result)
{
$dStart = date(‘d’, strtotime($start));
$YM = substr($start, 0, 8);
After all…it is OK to have a multi-dimensional array…I just use a foreach loop later.
There is one last thing.
Your code does not work with an end date(you just use a number of days…$idays).
The app I am working deals with an end date in the form of 2016-03-1.
What do you propose?
The only thing I have come up is to make a calculation between startdate and enddata to find how many days separates them and then use this as the value of the $idays variable.
array_map will be OK after all… I am OK with a multidimensional array.
There is one last issue though with your code…something I just noticed…more specifically with the date filter function:
function dateFilter(array $daysOfTheWeek)
{
return function ($date) use ($daysOfTheWeek) {
return in_array($date->format('l'), $daysOfTheWeek);
};
}
Its because the values in $closed_days are in lower case. E.g ['sunday', 'saturday']. dateFilter makes a comparison using $date->format('l') which returns the day with a capital letter. I.e Sunday. This causes the comparison to fail as in effect you have ended up with.
// Will return false
in_array('sunday', ['Sunday', 'Saturday']);
When you actually want.
// Will return true
in_array('Sunday', ['Sunday', 'Saturday']);
There are two ways to fix this. The first is to ensure that $closed_days uses capitals when it is passed to dateFilter. If this is not possible and you can only pass lowercase values then you will need to adapt dateFilter to make a comparision using lower case.
function dateFilter(array $daysOfTheWeek)
{
return function ($date) use ($daysOfTheWeek) {
return in_array(strtolower($date->format('l')), $daysOfTheWeek);
};
}