How we can get specific days from month within given date range in PHP?
If you are facing problem with "to get specific days from two months". I have added below solution that are working fine for me. Hope this will useful for you.
$cdate = date('d-m-Y'); //this is current date
$date = strtotime('+2 months'); //this is date after two months.
$nextDate = date('d-m-Y', $date);
$dates = dateRange($cdate, $nextDate);
function dateRange($begin, $end, $interval = null){
$begin = new DateTime($begin);
$end = new DateTime($end);
// Because DatePeriod does not the last date specified.
$end = $end->modify('+1 day');
$interval = new DateInterval($interval ? $interval : 'P1D');
return iterator_to_array(new DatePeriod($begin, $interval, $end));
}
$days = array_filter($dates, function ($date) {
$day = $date->format("l");
return $day === 'Tuesday' || $day === 'Thursday' || $day === 'Sunday';
});
$arrayDays = array();
foreach ($days as $date) {
$arrayDays[] = $date->format("d-m-Y");
}
//print Output
print_r($arrayDays);
This is show Tuesday, Thursday, Sunday date from 2 months.
No comments:
Post a Comment