Created
October 26, 2021 10:24
-
-
Save verdipratama/1df59553746fa1bf1f37a8f66a9e81d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_week_dates($month) | |
{ | |
$start = new DateTime($month.'-01'); | |
$end = new DateTime(); | |
$end->setDate($start->format('Y'), $start->format('m'), $start->format('t')); | |
$interval = DateInterval::createFromDateString('1 day'); | |
$period = new DatePeriod($start, $interval, $end); | |
$weeks = array(); | |
$week_key = 0; | |
$first = true; | |
foreach ($period as $dt) { | |
if ($first) { | |
$weeks[$week_key]['start'] = $dt->format('Y-m-d'); | |
$first = false; | |
} | |
if ($dt->format('D') == 'Sun' || $dt->format('d') == $end->format('d')) { | |
$weeks[$week_key]['end'] = $dt->format('Y-m-d'); | |
$week_key++; | |
$first = true; | |
} | |
} | |
return $weeks; | |
} | |
$weeks = get_week_dates('2021-01'); | |
var_dump($weeks); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment