All Data Structures Functions Variables Pages
DateTimeUtil.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
42  {
43  const DATETIME_FORMAT_DATE_WIDTH = 'short';
44 
45  const DATETIME_FORMAT_TIME_WIDTH = 'short';
46 
47  const DISPLAY_FORMAT_FOR_INPUT = 'Display Format For Input';
48 
49  const DISPLAY_FORMAT_ABBREVIATED_MONTH_ONLY_WIDTH = 'Display Format Abbreviated Month';
50 
51  const DISPLAY_FORMAT_ABBREVIATED_MONTH_AND_DAY_WIDTH = 'Display Format Abbreviated Month And Day';
52 
59  public static function getTimeSinceDisplayContent($dateTime, $durationDisplayFormat = false)
60  {
61  assert('DateTimeUtil::isValidDbFormattedDateTime($dateTime)');
62  $nowTimeStamp = time();
63  $dateTimeStamp = DateTimeUtil::convertDbFormatDateTimeToTimeStamp($dateTime);
64  $timeSinceLatestUpdate = $nowTimeStamp - $dateTimeStamp;
65  $timeForString = array(
66  'days' => floor($timeSinceLatestUpdate / 86400),
67  'hours' => floor($timeSinceLatestUpdate / 3600),
68  'minutes' => floor($timeSinceLatestUpdate / (60)),
69  'seconds' => floor($timeSinceLatestUpdate)
70  );
71 
72  if ($timeForString['days'] >= 1)
73  {
74  if ($durationDisplayFormat === true)
75  {
76  return Zurmo::t('Core', '{n} day|{n} days', $timeForString['days']);
77  }
78  else
79  {
80  return Zurmo::t('Core', '{n} day ago|{n} days ago', $timeForString['days']);
81  }
82  }
83  else
84  {
85  if ($timeForString['hours'] >= 1)
86  {
87  if ($durationDisplayFormat === true)
88  {
89  return Zurmo::t('Core', '{n} hour|{n} hours', $timeForString['hours']);
90  }
91  else
92  {
93  return Zurmo::t('Core', '{n} hour ago|{n} hours ago', $timeForString['hours']);
94  }
95  }
96  else
97  {
98  if ($timeForString['minutes'] >= 1)
99  {
100  if ($durationDisplayFormat === true)
101  {
102  return Zurmo::t('Core', '{n} minute|{n} minutes', $timeForString['minutes']);
103  }
104  else
105  {
106  return Zurmo::t('Core', '{n} minute ago|{n} minutes ago', $timeForString['minutes']);
107  }
108  }
109  else
110  {
111  if ($durationDisplayFormat === true)
112  {
113  return Zurmo::t('Core', '{n} second|{n} seconds', $timeForString['seconds']);
114  }
115  else
116  {
117  return Zurmo::t('Core', '{n} second ago|{n} seconds ago', $timeForString['seconds']);
118  }
119  }
120  }
121  }
122  }
123 
129  public static function getTimeSinceInDays($dateTime)
130  {
131  assert('DateTimeUtil::isValidDbFormattedDateTime($dateTime)');
132  $nowTimeStamp = time();
133  $dateTimeStamp = DateTimeUtil::convertDbFormatDateTimeToTimeStamp($dateTime);
134  $timeSince = $nowTimeStamp - $dateTimeStamp;
135  return floor($timeSince / 86400);
136  }
137 
143  public static function getMonthName($month)
144  {
145  if ($month != null)
146  {
147  return Yii::app()->locale->getMonthName((int)$month);
148  }
149  return $month;
150  }
151 
156  public static function getLocaleDateTimeFormat()
157  {
158  $dateTimePattern = Yii::app()->locale->getDateTimeFormat();
159  $timeFormat = Yii::app()->locale->getTimeFormat(self::DATETIME_FORMAT_TIME_WIDTH);
160  $dateFormat = Yii::app()->locale->getDateFormat(self::DATETIME_FORMAT_DATE_WIDTH);
161  return strtr($dateTimePattern, array('{0}' => $timeFormat, '{1}' => $dateFormat));
162  }
163 
164  public static function getLocaleDateTimeFormatForInput()
165  {
166  $dateTimePattern = Yii::app()->locale->getDateTimeFormat();
167  $timeFormat = Yii::app()->locale->getTimeFormat(self::DATETIME_FORMAT_TIME_WIDTH);
168  $dateFormat = Yii::app()->locale->getDateFormatForInput(self::DATETIME_FORMAT_DATE_WIDTH);
169  return strtr($dateTimePattern, array('{0}' => $timeFormat, '{1}' => $dateFormat));
170  }
171 
172  public static function getLocaleDateFormat($dateWidth = self::DATETIME_FORMAT_DATE_WIDTH)
173  {
174  return Yii::app()->locale->getDateFormat($dateWidth);
175  }
176 
177  public static function getLocaleDateFormatForInput($dateWidth = self::DATETIME_FORMAT_DATE_WIDTH)
178  {
179  return Yii::app()->locale->getDateFormatForInput($dateWidth);
180  }
181 
182  public static function getLocaleTimeFormat($timeWidth = self::DATETIME_FORMAT_TIME_WIDTH)
183  {
184  return Yii::app()->locale->getTimeFormat($timeWidth);
185  }
186 
187  public static function isLocaleTimeDisplayedAs12Hours()
188  {
189  $timeFormat = DateTimeUtil::getLocaleTimeFormat();
190  if (strpos($timeFormat, 'H') === false)
191  {
192  return true;
193  }
194  return false;
195  }
196 
197  public static function resolveTimeStampForDateTimeLocaleFormattedDisplay($value,
198  $dateWidth = self::DATETIME_FORMAT_DATE_WIDTH,
199  $timeWidth = self::DATETIME_FORMAT_TIME_WIDTH,
200  $forInput = false)
201  {
202  if ($value == null)
203  {
204  return null;
205  }
206  if ($forInput)
207  {
208  $pattern = static::getLocaleDateTimeFormatForInput();
209  return Yii::app()->dateFormatter->format($pattern, $value);
210  }
211  return Yii::app()->dateFormatter->formatDateTime($value, $dateWidth, $timeWidth);
212  }
213 
222  $date, $displayFormat = null)
223  {
224  if ($date == null)
225  {
226  return null;
227  }
228  $parsedTimeStamp = CDateTimeParser::parse($date, DatabaseCompatibilityUtil::getDateFormat());
229  if ($parsedTimeStamp === false)
230  {
231  return null;
232  }
233  if ($displayFormat == self::DISPLAY_FORMAT_ABBREVIATED_MONTH_ONLY_WIDTH)
234  {
235  $month = Yii::app()->dateFormatter->format('M', $parsedTimeStamp);
236  return Yii::app()->locale->getMonthName($month, 'abbreviated', true);
237  }
238  elseif ($displayFormat == self::DISPLAY_FORMAT_ABBREVIATED_MONTH_AND_DAY_WIDTH)
239  {
240  $month = Yii::app()->dateFormatter->format('M', $parsedTimeStamp);
241  $content = Yii::app()->locale->getMonthName($month, 'abbreviated', true);
242  $content .= ' ' . Yii::app()->dateFormatter->format('d', $parsedTimeStamp);
243  return $content;
244  }
245  elseif ($displayFormat == static::DISPLAY_FORMAT_FOR_INPUT)
246  {
247  return Yii::app()->dateFormatter->format(static::getLocaleDateFormatForInput(self::DATETIME_FORMAT_DATE_WIDTH), $parsedTimeStamp);
248  }
249  return Yii::app()->dateFormatter->format(static::getLocaleDateFormat(self::DATETIME_FORMAT_DATE_WIDTH), $parsedTimeStamp);
250  }
251 
252  public static function resolveValueForDateDBFormatted($value)
253  {
254  if ($value == null)
255  {
256  return null;
257  }
258  $localeDateFormat = DateTimeUtil::getLocaleDateFormatForInput();
259  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
260  CDateTimeParser::parse($value, $localeDateFormat));
261  }
262 
266  public static function convertFromUtcUnixStampByTimeZone($utcTimeStamp, $timeZone)
267  {
268  assert('is_string($timeZone)');
269  $timeZoneObject = new DateTimeZone($timeZone);
270  $offset = $timeZoneObject->getOffset(new DateTime());
271  return $utcTimeStamp + $offset;
272  }
273 
277  public static function convertFromLocalUnixStampByTimeZoneToUtcUnixStamp($utcTimeStamp, $timeZone)
278  {
279  assert('is_string($timeZone)');
280  $timeZoneObject = new DateTimeZone($timeZone);
281  $offset = $timeZoneObject->getOffset(new DateTime());
282  return $utcTimeStamp - $offset;
283  }
284 
285  public static function convertTimestampToDbFormatDate($timestamp)
286  {
287  assert('is_int($timestamp)');
288  $timeZone = date_default_timezone_get();
289  date_default_timezone_set('GMT');
290  $result = Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
291  $timestamp);
292  date_default_timezone_set($timeZone);
293  return $result;
294  }
295 
296  public static function convertTimestampToDbFormatDateTime($timestamp)
297  {
298  assert('is_int($timestamp)');
299  $timeZone = date_default_timezone_get();
300  date_default_timezone_set('GMT');
301  $result = Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateTimeFormat(),
302  $timestamp);
303  date_default_timezone_set($timeZone);
304  return $result;
305  }
306 
307  public static function convertDbFormatDateTimeToTimestamp($dbFormatDateTime)
308  {
309  assert('is_string($dbFormatDateTime)');
310  $timeZone = date_default_timezone_get();
311  date_default_timezone_set('GMT');
312  $result = strtotime($dbFormatDateTime);
313  date_default_timezone_set($timeZone);
314  return $result;
315  }
316 
317  public static function convertTimestampToDisplayFormat($timestamp,
318  $dateWidth = self::DATETIME_FORMAT_DATE_WIDTH,
319  $timeWidth = self::DATETIME_FORMAT_TIME_WIDTH,
320  $forInput = false)
321  {
322  assert('is_int($timestamp)');
323  return self::resolveTimeStampForDateTimeLocaleFormattedDisplay($timestamp, $dateWidth, $timeWidth, $forInput);
324  }
325 
326  public static function isValidDbFormattedDate($date) // Basic version, feel free to enhance.
327  {
328  if ($date == '0000-00-00')
329  {
330  return true;
331  }
332  return preg_match('/^[1-2][0-9][0-9][0-9]-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|[3][0-1])$/',
333  $date) == 1;
334  }
335 
336  public static function isValidDbFormattedDateTime($datetime) // Basic version, feel free to enhance.
337  {
338  if ($datetime == '0000-00-00 00:00:00')
339  {
340  return true;
341  }
342  return preg_match( '/^[1-2][0-9][0-9][0-9]-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|[3][0-1]) ' .
343  '(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/',
344  $datetime) == 1;
345  }
346 
347  public static function convertDbFormattedDateTimeToLocaleFormattedDisplay($dbFormatDateTime,
348  $dateWidth = self::DATETIME_FORMAT_DATE_WIDTH,
349  $timeWidth = self::DATETIME_FORMAT_TIME_WIDTH,
350  $forInput = false)
351  {
352  assert('is_string($dbFormatDateTime) || $dbFormatDateTime == null');
353  if ($dbFormatDateTime == null || $dbFormatDateTime == '0000-00-00 00:00:00')
354  {
355  return null;
356  }
357  $timestamp = self::convertDbFormatDateTimeToTimestamp($dbFormatDateTime);
358  return self::convertTimestampToDisplayFormat((int)$timestamp, $dateWidth, $timeWidth, $forInput);
359  }
360 
367  {
368  assert('is_string($localeFormattedDateTime) || $localeFormattedDateTime == null');
369  if ($localeFormattedDateTime == null)
370  {
371  return null;
372  }
373  $timestamp = CDateTimeParser::parse($localeFormattedDateTime, self::getLocaleDateTimeFormatForInput());
374  if ($timestamp == null)
375  {
376  return null;
377  }
378  $dbFormattedDateTime = self::convertTimestampToDbFormatDateTime($timestamp);
379  //todo deal with potential diffferent db format
380  return substr_replace($dbFormattedDateTime, '00', -2, 2);
381  }
382 
390  public static function convertDateIntoTimeZoneAdjustedDateTimeBeginningOfDay($dateValue)
391  {
392  assert('is_string($dateValue) && DateTimeUtil::isValidDbFormattedDate($dateValue)');
393  $greaterThanValue = $dateValue . ' 00:00:00';
394  return static::convertDbFormattedDateTimeToTimeZoneAdjustedDateTime($greaterThanValue);
395  }
396 
405  public static function convertDateIntoTimeZoneAdjustedDateTimeEndOfDay($dateValue)
406  {
407  assert('is_string($dateValue) && DateTimeUtil::isValidDbFormattedDate($dateValue)');
408  $lessThanValue = $dateValue . ' 23:59:59';
409  return static::convertDbFormattedDateTimeToTimeZoneAdjustedDateTime($lessThanValue);
410  }
411 
417  {
418  assert('is_string($dateTime) && DateTimeUtil::isValidDbFormattedDateTime($dateTime)');
419  $userTimeZone = new DateTimeZone(Yii::app()->timeZoneHelper->getForCurrentUser());
420  $adjustedDate = new DateTime($dateTime, $userTimeZone);
421  $adjustedDate->setTimezone(new DateTimeZone('GMT'));
422  $adjustedTimeStamp = $adjustedDate->getTimestamp();
423  return static::convertTimestampToDbFormatDateTime($adjustedTimeStamp);
424  }
425 
426  public static function getFirstDayOfAMonthDate($stringTime = null)
427  {
428  assert('is_string($stringTime) || $stringTime == null');
429  $dateTime = new DateTime($stringTime);
430  $dateTime->modify('first day of this month');
431  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
432  $dateTime->getTimestamp());
433  }
434 
435  public static function getLastDayOfAMonthDate($stringTime = null)
436  {
437  assert('is_string($stringTime) || $stringTime == null');
438  $dateTime = new DateTime($stringTime);
439  $dateTime->modify('last day of this month');
440  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
441  $dateTime->getTimestamp());
442  }
443 
444  public static function getFirstDayOfLastMonthDate($stringTime = null)
445  {
446  assert('is_string($stringTime) || $stringTime == null');
447  $dateTime = new DateTime($stringTime);
448  $dateTime->modify('first day of last month');
449  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
450  $dateTime->getTimestamp());
451  }
452 
453  public static function getLastDayOfLastMonthDate($stringTime = null)
454  {
455  assert('is_string($stringTime) || $stringTime == null');
456  $dateTime = new DateTime($stringTime);
457  $dateTime->modify('last day of last month');
458  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
459  $dateTime->getTimestamp());
460  }
461 
462  public static function getFirstDayOfNextMonthDate($stringTime = null)
463  {
464  assert('is_string($stringTime) || $stringTime == null');
465  $dateTime = new DateTime($stringTime);
466  $dateTime->modify('first day of next month');
467  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
468  $dateTime->getTimestamp());
469  }
470 
471  public static function getLastDayOfNextMonthDate($stringTime = null)
472  {
473  assert('is_string($stringTime) || $stringTime == null');
474  $dateTime = new DateTime($stringTime);
475  $dateTime->modify('last day of next month');
476  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
477  $dateTime->getTimestamp());
478  }
479 
480  public static function isDateValueNull(RedBeanModel $model, $attributeName)
481  {
482  assert('is_string($attributeName) || $attributeName == null');
483  return self::isDateStringNull($model->$attributeName);
484  }
485 
486  public static function isDateStringNull($date)
487  {
488  assert('is_string($date) || $date == null');
489  if ($date != null && $date != '0000-00-00')
490  {
491  return false;
492  }
493  return true;
494  }
495 
496  public static function isDateTimeValueNull(RedBeanModel $model, $attributeName)
497  {
498  assert('is_string($attributeName) || $attributeName == null');
499  return self::isDateTimeStringNull($model->$attributeName);
500  }
501 
502  public static function isDateTimeStringNull($dateTime)
503  {
504  assert('is_string($dateTime) || $dateTime == null');
505  if ($dateTime != null && $dateTime != '0000-00-00 00:00:00')
506  {
507  return false;
508  }
509  return true;
510  }
511 
512  public static function resolveDateAsDateTime($date)
513  {
514  assert('is_string($date)');
515  return $date . ' 00:00:00';
516  }
517 
518  public static function resolveDateTimeAsDate($dateTime)
519  {
520  assert('is_string($dateTime)');
521  if ($dateTime == '0000-00-00 00:00:00')
522  {
523  return '0000-00-00';
524  }
525  elseif ($dateTime == null)
526  {
527  return null;
528  }
529  return substr($dateTime, 0, 10);
530  }
531 
538  public static function getDatesBetweenTwoDatesInARange($beginDate, $endDate)
539  {
540  assert('is_string($beginDate)');
541  assert('is_string($endDate)');
542  $beginTimeStamp = strtotime($beginDate);
543  $endTimeStamp = strtotime($endDate);
544  $quantityOfDays = round(($endTimeStamp - $beginTimeStamp) / 86400) + 1;
545  $daysData = array();
546  for ($i = 0; $i < $quantityOfDays; $i++)
547  {
548  $daysData[] = date('Y-m-d', ($beginTimeStamp + ($i * 86400)));
549  }
550  return $daysData;
551  }
552 
560  public static function getWeekStartAndEndDatesBetweenTwoDatesInARange($beginDate, $endDate)
561  {
562  assert('is_string($beginDate)');
563  assert('is_string($endDate)');
564  $weekTimeStamp = strtotime($beginDate);
565  $endTimeStamp = strtotime($endDate);
566  $weeksData = array();
567  while ($weekTimeStamp <= $endTimeStamp)
568  {
569  $date = new DateTime(date('Y-m-d', $weekTimeStamp));
570  if (('Sunday' == $date->format('l')) && !static::doesWeekStrictlyStartOnMonday())
571  {
572  $date->modify('Monday last week');
573  }
574  else
575  {
576  $date->modify('Monday this week');
577  }
578  $beginDateOfWeek = $date->format('Y-m-d');
579  $date->modify('this week next sunday');
580  $endDateOfWeek = $date->format('Y-m-d');
581  $weeksData[$beginDateOfWeek] = $endDateOfWeek;
582  $weekTimeStamp = strtotime("+1 week", $weekTimeStamp); // Not Coding Standard
583  }
584  //Capture dates in last week if needed
585  $date = new DateTime(date('Y-m-d', $weekTimeStamp));
586  if (('Sunday' == $date->format('l')) && !static::doesWeekStrictlyStartOnMonday())
587  {
588  $date->modify('Monday last week');
589  }
590  else
591  {
592  $date->modify('Monday this week');
593  }
594  $beginDateOfWeek = $date->format('Y-m-d');
595  if ($beginDateOfWeek < $endDate)
596  {
597  $date->modify('this week next sunday');
598  $endDateOfWeek = $date->format('Y-m-d');
599  $weeksData[$beginDateOfWeek] = $endDateOfWeek;
600  $weekTimeStamp = strtotime("+1 week", $weekTimeStamp); // Not Coding Standard
601  }
602  if ($endDateOfWeek < $endDate)
603  {
604  $date = new DateTime(date('Y-m-d', $weekTimeStamp));
605  if (('Sunday' == $date->format('l')) && !static::doesWeekStrictlyStartOnMonday())
606  {
607  $date->modify('Monday last week');
608  }
609  else
610  {
611  $date->modify('Monday this week');
612  }
613  $beginDateOfWeek = $date->format('Y-m-d');
614  $date->modify('this week next sunday');
615  $endDateOfWeek = $date->format('Y-m-d');
616  $weeksData[$beginDateOfWeek] = $endDateOfWeek;
617  }
618  return $weeksData;
619  }
620 
628  public static function getMonthStartAndEndDatesBetweenTwoDatesInARange($beginDate, $endDate)
629  {
630  assert('is_string($beginDate)');
631  assert('is_string($endDate)');
632  $monthTimeStamp = strtotime($beginDate);
633  $endTimeStamp = strtotime($endDate);
634  $monthsData = array();
635  while ($monthTimeStamp <= $endTimeStamp)
636  {
637  $date = new DateTime(date('Y-m-d', $monthTimeStamp));
638  $date->modify('first day of this month');
639  $beginDateOfMonth = $date->format('Y-m-d');
640  $date->modify('last day of this month');
641  $endDateOfMonth = $date->format('Y-m-d');
642  $monthsData[$beginDateOfMonth] = $endDateOfMonth;
643  $monthTimeStamp = strtotime("+1 month", $monthTimeStamp); // Not Coding Standard
644  }
645  //Capture dates in last month if needed
646  $date = new DateTime(date('Y-m-d', $monthTimeStamp));
647  $date->modify('first day of this month');
648  $beginDateOfMonth = $date->format('Y-m-d');
649  if ($beginDateOfMonth < $endDate)
650  {
651  $date->modify('last day of this month');
652  $endDateOfMonth = $date->format('Y-m-d');
653  $monthsData[$beginDateOfMonth] = $endDateOfMonth;
654  $monthTimeStamp = strtotime("+1 month", $monthTimeStamp); // Not Coding Standard
655  }
656  if ($endDateOfMonth < $endDate)
657  {
658  $date = new DateTime(date('Y-m-d', $monthTimeStamp));
659  $date->modify('first day of this month');
660  $beginDateOfMonth = $date->format('Y-m-d');
661  $date->modify('last day of this month');
662  $endDateOfMonth = $date->format('Y-m-d');
663  $monthsData[$beginDateOfMonth] = $endDateOfMonth;
664  }
665  return $monthsData;
666  }
667 
673  public static function getFirstDayOfAWeek($stringTime = null)
674  {
675  assert('is_string($stringTime) || $stringTime == null');
676  $dayOfTheWeek = date('w', strtotime($stringTime));
677  $dateTime = new DateTime($stringTime);
678  $dateTime->modify('this week');
679  if ($dayOfTheWeek == 0 && !static::doesWeekStrictlyStartOnMonday())
680  {
681  $dateTime->modify('-7 days');
682  }
683  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
684  $dateTime->getTimestamp());
685  }
686 
692  public static function getLastDayOfAWeek($stringTime = null)
693  {
694  assert('is_string($stringTime) || $stringTime == null');
695  $dayOfTheWeek = date('w', strtotime($stringTime));
696  $dateTime = new DateTime($stringTime);
697  $dateTime->modify('this week +6 days');
698  if ($dayOfTheWeek == 0 && !static::doesWeekStrictlyStartOnMonday())
699  {
700  $dateTime->modify('-7 days');
701  }
702  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
703  $dateTime->getTimestamp());
704  }
705 
710  public static function getTodaysDate()
711  {
712  $dateTime = new DateTime();
713  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
714  $dateTime->getTimestamp());
715  }
716 
721  public static function getFirstDayOfNextWeek()
722  {
723  $dateTime = new DateTime();
724  $dateTime->modify('next monday');
725  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
726  $dateTime->getTimestamp());
727  }
728 
733  public static function getTomorrowsDate()
734  {
735  $dateTime = new DateTime();
736  $dateTime->modify('tomorrow');
737  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
738  $dateTime->getTimestamp());
739  }
740 
746  public static function convertDateToDateTimeByTimeZoneOffset($date)
747  {
749  $absOffset = abs($offset);
750  $hours = floor($absOffset / (60 * 60));
751  $absOffset -= $hours * (60 * 60);
752 
753  $minutes = floor($absOffset / 60);
754  $absOffset -= $minutes * 60;
755 
756  $seconds = floor($absOffset);
757  $absOffset -= $seconds;
758  $hours = ($hours < 10)? '0' . $hours : $hours;
759  $minutes = ($minutes < 10)? '0' . $minutes : $minutes;
760  $seconds = ($seconds < 10)? '0' . $seconds : $seconds;
761  return $date . ' ' . $hours . ':' . $minutes . ':' . $seconds;
762  }
763 
769  public static function doesWeekStrictlyStartOnMonday()
770  {
771  // 2014-04-27 was a sunday, so if we consider strictly that a week starts on monday,
772  // then the first day of the week is 2014-04-21
773  $stringTime = '2014-04-27 23:59:59';
774  $dateTime = new DateTime($stringTime);
775  $dateTime->modify('this week');
776  if ($dateTime->format('Y-m-d') == '2014-04-21')
777  {
778  return true;
779  }
780  else
781  {
782  return false;
783  }
784  }
785  }
786 ?>
static getTomorrowsDate()
static convertFromLocalUnixStampByTimeZoneToUtcUnixStamp($utcTimeStamp, $timeZone)
static getFirstDayOfNextWeek()
static convertDateToDateTimeByTimeZoneOffset($date)
static getMonthName($month)
static getTodaysDate()
static getTimeSinceDisplayContent($dateTime, $durationDisplayFormat=false)
static convertDateTimeLocaleFormattedDisplayToDbFormattedDateTimeWithSecondsAsZero($localeFormattedDateTime)
static getLocaleDateTimeFormat()
static convertDateIntoTimeZoneAdjustedDateTimeBeginningOfDay($dateValue)
static getFirstDayOfAWeek($stringTime=null)
static getLastDayOfAWeek($stringTime=null)
static convertDbFormattedDateTimeToTimeZoneAdjustedDateTime($dateTime)
static getTimeSinceInDays($dateTime)
static resolveValueForDateLocaleFormattedDisplay($date, $displayFormat=null)
static getMonthStartAndEndDatesBetweenTwoDatesInARange($beginDate, $endDate)
static doesWeekStrictlyStartOnMonday()
static getDatesBetweenTwoDatesInARange($beginDate, $endDate)
static convertDateIntoTimeZoneAdjustedDateTimeEndOfDay($dateValue)
static getWeekStartAndEndDatesBetweenTwoDatesInARange($beginDate, $endDate)
static convertFromUtcUnixStampByTimeZone($utcTimeStamp, $timeZone)
Generated on Wed Feb 26 2020 07:10:35