Skip to main content

Posts

Showing posts from March, 2023

Find the dates ( month start and end dates) between the given two dates using PHP

When you want to pull the dates between two dates, where the between dates are connecting dots. If you want to list the complete date ranges through months. For example, to find the dates between below dates date1 = '2022-12-29'; date2 = '2023-02-20'; expected output would be  Start Date End Date 2022-12-29 2022-12-31 2023-01-01 2023-01-31 2023-02-01 2023-02-20 <?php /* * Returns array of month's start & end dates between provided dates. * * @param string $start_date * @param string $end_date * * * @return array */ function getDates($start_date, $end_date){ $start_date_obj = new DateTime($start_date); $end_date_obj = new DateTime($end_date); $diff = $end_date_obj->diff($start_date_obj); $months = (($diff->y) * 12) + ($diff->m); $dates[] = array('start_date' => $start_date, 'end_date' => date('Y-m-t', strtotime($start_date)), 'suffix' =...