Framework  3.9
date_utils.inc
Go to the documentation of this file.
1 <?php
2 /**************************************************************
3 
4  Copyright (c) 2007-2010 Sonjara, Inc
5 
6  Permission is hereby granted, free of charge, to any person
7  obtaining a copy of this software and associated documentation
8  files (the "Software"), to deal in the Software without
9  restriction, including without limitation the rights to use,
10  copy, modify, merge, publish, distribute, sublicense, and/or sell
11  copies of the Software, and to permit persons to whom the
12  Software is furnished to do so, subject to the following
13  conditions:
14 
15  The above copyright notice and this permission notice shall be
16  included in all copies or substantial portions of the Software.
17 
18  Except as contained in this notice, the name(s) of the above
19  copyright holders shall not be used in advertising or otherwise
20  to promote the sale, use or other dealings in this Software
21  without prior written authorization.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30  OTHER DEALINGS IN THE SOFTWARE.
31 
32 *****************************************************************/
33 
38 class DateUtils
39 {
40  function DateUtils()
41  {
42 
43  }
44 
52  static function getMonthDays($m, $y)
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  }
60 
61 
68  static function getMonthWeekDays($month, $year)
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  }
84 
85  /*
86  * If start and end dates are the same date
87  * then show the date followed by start and end time.
88  * Otherwise, show start date and time to end date
89  * and time.
90  *
91  * $start - datetime
92  * $end - datetime
93  */
94  static function formatStartDateToEndDate($start, $end)
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) ." &ndash; ". 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 &ndash; $e_time";
116  }
117  }
118 
126  static function addMonths(&$date, $months)
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  }
143 
151  static function addYears(&$date, $years)
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  }
162 
168  static function fuzzyDate($date)
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  }
210 
216  static function getMonths()
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  }
225 }
DateUtils - provides support for date/time manipulation.
Definition: date_utils.inc:39
static getMonthWeekDays($month, $year)
Get the number of week days in the specified month.
Definition: date_utils.inc:68
static formatStartDateToEndDate($start, $end)
Definition: date_utils.inc:94
static fuzzyDate($date)
Fuzzy approximates of some time in the past.
Definition: date_utils.inc:168
static addMonths(&$date, $months)
DateTime will fail to increment month correctly when current date is last day of 31 day month.
Definition: date_utils.inc:126
static getMonthDays($m, $y)
Returns the number of days in the specified month.
Definition: date_utils.inc:52
static getMonths()
Return an array of month names in the currently selected locale, indexed by month number.
Definition: date_utils.inc:216
static addYears(&$date, $years)
DateTime will fail to increment year correctly in some instances.
Definition: date_utils.inc:151
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