Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
datetimefactory.php
1<?php
2
4
6
8{
13 public static function createToday(): DateTime
14 {
15 $dateInUserTimezone = new Main\Type\DateTime();
16 $dateInUserTimezone
17 ->toUserTime()
18 ->setTime(0, 0, 0)
19 ;
20
21 return new DateTime($dateInUserTimezone->getTimestamp());
22 }
23
28 public static function createCurrentWeekMonday(): DateTime
29 {
30 $date = self::getMonday();
31
32 return new DateTime($date->getTimestamp());
33 }
34
39 public static function createNextWeekMonday(): DateTime
40 {
41 $date = self::getMonday();
42 $date->add('+1 week');
43
44 return new DateTime($date->getTimestamp());
45 }
46
51 public static function createLastWeekMonday(): DateTime
52 {
53 $date = self::getMonday();
54 $date->add('-1 week');
55
56 return new DateTime($date->getTimestamp());
57 }
58
63 public static function createFirstDayOfCurrentMonth(): DateTime
64 {
65 $date = self::getFirstDayOfMonth(strtotime('first day of this month'));
66
67 return new DateTime($date->getTimestamp());
68 }
69
74 public static function createFirstDayOfNextMonth()
75 {
76 $date = self::getFirstDayOfMonth(strtotime('first day of this month'));
77 $date->add('+1 month');
78
79 return new DateTime($date->getTimestamp());
80 }
81
86 public static function createFirstDayOfLastMonth()
87 {
88 $date = self::getFirstDayOfMonth(strtotime('first day of this month'));
89 $date->add('-1 month');
90
91 return new DateTime($date->getTimestamp());
92 }
93
94 private static function getMonday(): Main\Type\DateTime
95 {
96 $timestamp = strtotime('monday this week');
97
98 $date = Main\Type\DateTime::createFromTimestamp($timestamp);
99 $date->setTime(0, 0, 0);
100
101 $todayDate = new Main\Type\DateTime();
102 $todayDate->setTime(0, 0, 0);
103
104 if ($date->format('Y-m-d') === $todayDate->format('Y-m-d')) // is it monday today?
105 {
106 $todayInUserTimezone = new Main\Type\DateTime();;
107 $todayInUserTimezone->toUserTime();
108
109 if ($todayInUserTimezone->format('N') == '7') // if user date is still previous saturday
110 {
111 $date->add('-1 week');
112 }
113 }
114
115 return $date;
116 }
117
118 private static function getFirstDayOfMonth(): Main\Type\DateTime
119 {
120 $timestamp = strtotime('first day of this month');
121
122 $date = Main\Type\DateTime::createFromTimestamp($timestamp);
123 $date->setTime(0, 0, 0);
124
125 $todayDate = new Main\Type\DateTime();
126 $todayDate->setTime(0, 0, 0);
127
128 if ($date->format('Y-m-d') === $todayDate->format('Y-m-d')) // is first day of month today?
129 {
130 $todayInUserTimezone = new Main\Type\DateTime();
131 $todayInUserTimezone->toUserTime();
132
133 if ((int)($todayInUserTimezone->format('j')) >= 28) // if user date is still previous month
134 {
135 $date->add('-1 month');
136 }
137 }
138
139 return $date;
140 }
141}