Skip to content

Instantly share code, notes, and snippets.

@menkaff
Last active September 1, 2021 11:56
Show Gist options
  • Save menkaff/657b4989cba110f9ee2e293362b20f5f to your computer and use it in GitHub Desktop.
Save menkaff/657b4989cba110f9ee2e293362b20f5f to your computer and use it in GitHub Desktop.
This function can generate start and end of week dates based on Jalali calendar , in Jalali calendar start week day is Saturday and Friday is the last day of a week. Persian description => این تابع جهت تولید روزهای میلادی یک ماه شمسی هست با احتساب شنبه و جمعه به عنوان روزهای ابتدایی و پایانی هفته
<?php
use Carbon\Carbon;
use Morilog\Jalali\Jalalian;
class GenerateJalaliWeekOfMonth
{
public function result($params)
{
$fromDate = \Morilog\Jalali\CalendarUtils::toGregorian($params['year'], $params['month'], 1);
$fromDate = Carbon::createFromDate($fromDate[0], $fromDate[1], $fromDate[2], 'Asia/Tehran')->format('Y-m-d');
$toDateDay = (new Jalalian($params['year'], $params['month'], 1))->getMonthDays();
$toDate = \Morilog\Jalali\CalendarUtils::toGregorian($params['year'], $params['month'], $toDateDay);
$toDate = Carbon::createFromDate($toDate[0], $toDate[1], $toDate[2], 'Asia/Tehran')->format('Y-m-d');
$weekDate = [];
$weekLimit = 5;
$firstDayOfMonth = strtotime($fromDate);
$firstDayOfMonth = strtolower(date('D', $firstDayOfMonth));
for ($i = 0; $i < $weekLimit; $i++) {
$j = $i * 7;
if ($firstDayOfMonth == "sat" || $firstDayOfMonth == "sun") {
$saturday = strtotime('saturday this week', strtotime(' +' . $j . ' day', strtotime($fromDate)));
$friday = strtotime('friday next week', strtotime(' +' . $j . ' day', strtotime($fromDate)));
} else {
$saturday = strtotime('saturday last week', strtotime(' +' . $j . ' day', strtotime($fromDate)));
$friday = strtotime('friday this week', strtotime(' +' . $j . ' day', strtotime($fromDate)));
}
$weekStart = date('Y-m-d', $saturday);
$weekEnd = date('Y-m-d', $friday);
if ($i == 0) {
$weekStart = $fromDate;
$weekDayStart = strtolower(date("D", strtotime($fromDate)));
$monthDays = (new Jalalian($params['year'], $params['month'], 1))->getMonthDays();
if ($monthDays == 31 && ($weekDayStart == "thu" || $weekDayStart == "fri")) {
$weekLimit = 6;
} elseif ($monthDays == 30 && $weekDayStart == "fri") {
$weekLimit = 6;
}
} elseif ($i == $weekLimit - 1) {
$weekEnd = $toDate;
}
$weekDate[$i]['weekStart'] = $weekStart;
$weekDate[$i]['weekEnd'] = $weekEnd;
}
return $weekDate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment