Skip to content

Instantly share code, notes, and snippets.

@crusj
Created August 23, 2019 06:42
Show Gist options
  • Save crusj/ab627a04a26122adb694e63208b4f340 to your computer and use it in GitHub Desktop.
Save crusj/ab627a04a26122adb694e63208b4f340 to your computer and use it in GitHub Desktop.
php月份按周分段,以及判断当前时间是当月的第几周
<?php
class Date {
//月转化为周
private function month2Week(int $year, int $month): array
{
$start = 1;
$end = date('d', strtotime(date("{$year}-{$month}-01") . ' + 1 month - 1 day'));
$days = range($start, $end);
return array_chunk($days, 7);
}
//时间戳判断当前是第几周
private function whichWeek(int $timeStamp): int
{
$date = date('Y-m-d', $timeStamp);
list($year, $month, $day) = explode('-', $date);
$weeks = $this->month2Week($year, $month);
foreach ($weeks as $key => $weekDays) {
if (in_array($day, $weekDays)) {
return $key + 1;
}
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment