Framework  3.9
DateUtils Class Reference

DateUtils - provides support for date/time manipulation. More...

Public Member Functions

 DateUtils ()
 

Static Public Member Functions

static getMonthDays ($m, $y)
 Returns the number of days in the specified month. More...
 
static getMonthWeekDays ($month, $year)
 Get the number of week days in the specified month. More...
 
static formatStartDateToEndDate ($start, $end)
 
static addMonths (&$date, $months)
 DateTime will fail to increment month correctly when current date is last day of 31 day month. More...
 
static addYears (&$date, $years)
 DateTime will fail to increment year correctly in some instances. More...
 
static fuzzyDate ($date)
 Fuzzy approximates of some time in the past. More...
 
static getMonths ()
 Return an array of month names in the currently selected locale, indexed by month number. More...
 

Detailed Description

DateUtils - provides support for date/time manipulation.

Definition at line 38 of file date_utils.inc.

Member Function Documentation

◆ addMonths()

static DateUtils::addMonths ( $date,
  $months 
)
static

DateTime will fail to increment month correctly when current date is last day of 31 day month.

Use addMonths instead.

Parameters
DateTimeobject $date
Number$months(can be positive or negative)

Definition at line 126 of file date_utils.inc.

127  {
128  $init = clone $date;
129  $modifier = $months.' months';
130  $back_modifier = -$months.' months';
131 
132  $date->modify($modifier);
133  $back_to_init = clone $date;
134  $back_to_init->modify($back_modifier);
135 
136  while($init->format('m') != $back_to_init->format('m'))
137  {
138  $date->modify('-1 day');
139  $back_to_init = clone $date;
140  $back_to_init->modify($back_modifier);
141  }
142  }

◆ addYears()

static DateUtils::addYears ( $date,
  $years 
)
static

DateTime will fail to increment year correctly in some instances.

Parameters
DateTimeobject $date
Number$years(can be positive or negative)

Definition at line 151 of file date_utils.inc.

152  {
153  $init = clone $date;
154  $modifier = $years.' years';
155  $date->modify($modifier);
156 
157  while($date->format('m')!=$init->format('m'))
158  {
159  $date->modify('-1 day');
160  }
161  }

◆ DateUtils()

DateUtils::DateUtils ( )

Definition at line 40 of file date_utils.inc.

41  {
42 
43  }

◆ formatStartDateToEndDate()

static DateUtils::formatStartDateToEndDate (   $start,
  $end 
)
static

Definition at line 94 of file date_utils.inc.

95  {
96  $yyyy = substr($start,0,4);
97  $yyyy_2 = substr($end,0,4);
98  $mm = substr($start,5,2);
99  $mm_2 = substr($end,5,2);
100  $dd = substr($start,8,2);
101  $dd_2 = substr($end,8,2);
102 
103  if($yyyy != $yyyy_2 || $mm != $mm_2 || $dd != $dd_2)
104  {
105  return formatDateTime12Hr($start) ." – ". formatDateTime12Hr($end);
106  }
107  else
108  {
109  $hh = substr($start,11,2);
110  $mi = substr($start,14,2);
111  $hh_2 = substr($end,11,2);
112  $mi_2 = substr($end,14,2);
113  $s_time = date("g:ia", mktime($hh,$mi));
114  $e_time = date("g:ia", mktime($hh_2,$mi_2));
115  return formatDateShort($start) . " $s_time – $e_time";
116  }
117  }
formatDateShort($date)
Utility function to format date in short form (MM/DD/YYYY), with no time component.
Definition: functions.inc:183
formatDateTime12Hr($datetime)
Definition: functions.inc:334

◆ fuzzyDate()

static DateUtils::fuzzyDate (   $date)
static

Fuzzy approximates of some time in the past.

Parameters
unknown$date
Returns
string

Definition at line 168 of file date_utils.inc.

169  {
170  $time = strtotime($date);
171  $now = time();
172  $ago = $now - $time;
173  if ($ago < 60)
174  {
175  $when = round($ago);
176  $s = ($when == 1)?"second":"seconds";
177  return "$when $s ago";
178  }
179  else if ($ago < 3600)
180  {
181  $when = round($ago / 60);
182  $m = ($when == 1)?"minute":"minutes";
183  return "$when $m ago";
184  }
185  else if ($ago >= 3600 && $ago < 86400)
186  {
187  $when = round($ago / 60 / 60);
188  $h = ($when == 1)?"hour":"hours";
189  return "$when $h ago";
190  }
191  else if ($ago >= 86400 && $ago < 2629743.83)
192  {
193  $when = round($ago / 60 / 60 / 24);
194  $d = ($when == 1)?"day":"days";
195  return "$when $d ago";
196  }
197  else if ($ago >= 2629743.83 && $ago < 31556926)
198  {
199  $when = round($ago / 60 / 60 / 24 / 30.4375);
200  $m = ($when == 1)?"month":"months";
201  return "$when $m ago";
202  }
203  else
204  {
205  $when = round($ago / 60 / 60 / 24 / 365);
206  $y = ($when == 1)?"year":"years";
207  return "$when $y ago";
208  }
209  }

◆ getMonthDays()

static DateUtils::getMonthDays (   $m,
  $y 
)
static

Returns the number of days in the specified month.

Parameters
number$mthe month number
number$ythe year
Returns
number the number of days in that month

Definition at line 52 of file date_utils.inc.

53  {
54  $monthDays = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
55  $d = $monthDays[$m - 1];
56  if ($m == 2 && ($y % 4) == 0 && ($y % 100) != 0) $d++;
57 
58  return $d;
59  }

◆ getMonths()

static DateUtils::getMonths ( )
static

Return an array of month names in the currently selected locale, indexed by month number.

This is a format suitable for use with a SelectFieldRenderer

Returns
array month names indexed by month number

Definition at line 216 of file date_utils.inc.

217  {
218  $months = array();
219  for($i = 1; $i < 13; ++$i)
220  {
221  $months[$i] = date("F", mktime(0, 0, 0, $i, 10));
222  }
223  return $months;
224  }

◆ getMonthWeekDays()

static DateUtils::getMonthWeekDays (   $month,
  $year 
)
static

Get the number of week days in the specified month.

Parameters
number$mthe month number
number$ythe year
Returns
number the number of week days in that month

Definition at line 68 of file date_utils.inc.

69  {
70  $weekDaysInMonth = array
71  (
72  28 => array(20, 20, 20, 20, 20, 20, 20),
73  29 => array(21, 21, 21, 21, 21, 20, 20),
74  30 => array(22, 22, 22, 22, 21, 20, 21),
75  31 => array(23, 23, 23, 22, 21, 21, 22)
76  );
77 
78  $days = getMonthDays($month, $year);
79  $date = strtotime("$year-$month-01");
80 
81  $first = date("N", $date) - 1;
82  return $weekDaysInMonth[$days][$first];
83  }
static getMonthDays($m, $y)
Returns the number of days in the specified month.
Definition: date_utils.inc:52

The documentation for this class was generated from the following file: