Back

getDatesBetweenMonthRange function

Copy Below Code View As A Text File Show Text Only Show API Edit Code
                            

/** * Get months array for loop in date format. * * This function takes two numbers as input and returns their sum. * * @param string $fromMonth start of the month. e.g. 06/2023 * @param string $toMonth end of the month. e.g. 08/2023 * * @return array dates array from $fromMonth to $toMonth. e.g array(2023-06-01,2023-07-01,2023-08-01) */ static function getDatesBetweenMonthRange($fromMonth, $toMonth) { if (!commonfunctions::isNullOrEmpty($fromMonth) && !commonfunctions::isNullOrEmpty($toMonth)) { $fromDateArr = explode('/', $fromMonth); $fromMonth = $fromDateArr[0] . '/01/' . $fromDateArr[1]; $toDateArr = explode('/', $toMonth); $toMonth = $toDateArr[0] . '/01/' . $toDateArr[1]; $fromMonth = date('Y-m-d', strtotime($fromMonth)); $toMonth = date('Y-m-d', strtotime($toMonth)); } $startDate = new DateTime($fromMonth); $endDate = new DateTime($toMonth); $currentDate = $startDate; $monthFilterData = array(); while ($currentDate <= $endDate) { $monthFilterData[] = $currentDate->format('Y-m-d'); $currentDate->add(new DateInterval('P1M')); } return $monthFilterData;//date array between fromMonth and toMonth }