-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDayOfWeek.php
More file actions
52 lines (45 loc) · 1.17 KB
/
DayOfWeek.php
File metadata and controls
52 lines (45 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace TinyBlocks\Time;
/**
* Represents a day of the week following ISO 8601, where Monday is 1 and Sunday is 7.
*/
enum DayOfWeek: int
{
case Monday = 1;
case Tuesday = 2;
case Wednesday = 3;
case Thursday = 4;
case Friday = 5;
case Saturday = 6;
case Sunday = 7;
/**
* Derives the day of the week from an Instant.
*
* @param Instant $instant The point in time to extract the day from.
* @return DayOfWeek The corresponding day of the week in UTC.
*/
public static function fromInstant(Instant $instant): DayOfWeek
{
$isoDay = (int)$instant->toDateTimeImmutable()->format('N');
return self::from($isoDay);
}
/**
* Checks whether this day falls on a weekday (Monday through Friday).
*
* @return bool True if this is a weekday.
*/
public function isWeekday(): bool
{
return $this->value <= 5;
}
/**
* Checks whether this day falls on a weekend (Saturday or Sunday).
*
* @return bool True if this is a weekend day.
*/
public function isWeekend(): bool
{
return $this->value >= 6;
}
}