Skip to content

Instantly share code, notes, and snippets.

@gladx
Last active January 1, 2018 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gladx/4746380399fd134fbd6005e28630e44f to your computer and use it in GitHub Desktop.
Save gladx/4746380399fd134fbd6005e28630e44f to your computer and use it in GitHub Desktop.
how get 1th and 7th day of week in persian
/**
* ورودی تابع آبجکت DateTime است
* خروجی این متد تاریخ اول و آخر هفته است
* اول هفته شنبه و آخر هفته جمعه
* خروجی نمونه
* [
* 'from' => 2018-1-6,
* 'to' => 2018-1-12
* ]
*/
public function getStartEndWeek($date = null)
{
if($date == null){
$date = new \DateTime('today', new \DateTimezone("Asia/Tehran"));
// $t = (new \DateTime('today'))->format('w');
}
$from = clone $date;
$to = clone $date;
$t = $date->format('w');
if($t != 6 ) {
//$t++;
$interval = new \DateInterval('P'. ($t + 1) .'D');
$from = ($from->sub($interval))->format('Y-m-d');
//$from = (new \DateTime('today -'. $t .'day', new \DateTimezone("Asia/Tehran")))->format('Y-m-d');
//$t = 6 - $t;
$interval = new \DateInterval('P'. (5 - $t) .'D');
$to = ($to->add($interval))->format('Y-m-d');
//$to = (new \DateTime('today +'. $t .'day', new \DateTimezone("Asia/Tehran")))->format('Y-m-d');
} else {
$from = $from->format('Y-m-d');
$interval = new \DateInterval('P6D');
$to = ($to->add($interval))->format('Y-m-d');
// $from = (new \DateTime('today', new \DateTimezone("Asia/Tehran")))->format('Y-m-d');
// $to = (new \DateTime('today +6 day', new \DateTimezone("Asia/Tehran")))->format('Y-m-d');
}
return [
'start' => $from,
'end' => $to
];
}
<?php
$t = (new \DateTime('today'))->format('w');
if($t != 6 ) {
$t++;
$from = (new \DateTime('today -'. $t .'day', new \DateTimezone("Asia/Tehran")))->format('Y-m-d');
$t = 6 - $t;
$to = (new \DateTime('today +'. $t .'day', new \DateTimezone("Asia/Tehran")))->format('Y-m-d');
} else {
$from = (new \DateTime('today', new \DateTimezone("Asia/Tehran")))->format('Y-m-d');
$to = (new \DateTime('today +6 day', new \DateTimezone("Asia/Tehran")))->format('Y-m-d');
}
<?php
$t = (new \DateTime('today'));
$f = new \DateTime('friday this week');
if ($t <= $f) {
$from = (new \DateTime('friday this week -6 day', new \DateTimezone("Asia/Tehran")))->format('Y-m-d');
$to = (new \DateTime('friday this week', new \DateTimezone("Asia/Tehran")))->format('Y-m-d');
} else {
$from = (new \DateTime('friday next week -6 day', new \DateTimezone("Asia/Tehran")))->format('Y-m-d');
$to = (new \DateTime('friday next week', new \DateTimezone("Asia/Tehran")))->format('Y-m-d');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment