.

GyroDate Class Reference
[Lib]

Common Date and DateTime functions. More...

List of all members.

Static Public Member Functions

static  add_days ($date, $days)
  Static.
static  add_months ($date, $months)
  Static.
static  add_workdays ($date, $days_to_add)
  Adds work days to given date.
static  convert_to_days ($timestamp)
  Returns days since 1.1.2000.
static  datetime ($string)
  Static.
static  day ($date)
  Casts date to day (That is 0:00:00).
static  get_day ($date)
  Returns day of given date.
static  get_month ($date)
  Returns number of month of given date.
static  get_week ($date)
  Return week in year of given Date.
static  get_weekday ($date)
  Return weekday of given Date.
static  get_year ($date)
  Returns year of given date.
static  http_date ($date)
  Converts timestamp to string used in HTTP-Header fields (such as "Expires").
static  is_holiday ($date)
  Returns true if given date is a holiday.
static  is_this_month ($time)
  Returns true if the given datetime is of this month.
static  is_this_year ($time)
  Returns true if the given datetime is of this year.
static  is_today ($time)
  Returns true if the given datetime is of today.
static  is_workday ($date)
  Returns if given date is a work day (that is: not saturday or sunday).
static  iso_date ($date)
  Static.
static  local_date ($date, $includetime=true)
  Static.
static  month ($date)
  Casts date to month (That is 1st, 0:00:00).
static  mysql_date ($date, $includetime=true)
  Static.
static  mysql_time ($date)
  Static.
static  parse ($date, $format)
  Calls strptime, but returns unix timestamp.
static  rfc_date ($date)
  Static.
static  set_day ($date, $day)
  Sets day on given date (keeps date).
static  set_time ($date, $hour, $min=0, $sec=0)
  Sets Time on given date (keeps date).
static  substract_days ($date, $days)
  Static.
static  substract_months ($date, $months)
  Static.
static  today ()

Public Attributes

const  DAY_MONTH_YEAR = 'DMY'
const  FRIDAY = 5
const  MONDAY = 1
const  MONTH_DAY_YEAR = 'MDY'
const  ONE_DAY = 86400
const  ONE_HOUR = 3600
const  ONE_MINUTE = 60
const  ONE_MONTH = 2592000
  30 days
const  ONE_WEEK = 604800
const  ONE_YEAR = 31536000
  365 days
const  SATURDAY = 6
const  SUNDAY = 0
const  THURSDAY = 4
const  TUESDAY = 2
const  WEDNESDAY = 3
const  YEAR_MONTH_DAY = 'YMD'

Static Public Attributes

static  $holidays = array()
static  $local_date_order = self::MONTH_DAY_YEAR
static  $non_workdays = array(self::SUNDAY, self::SATURDAY)

Detailed Description

Common Date and DateTime functions.

Author:
Gerd Riesselmann

Definition at line 8 of file date.cls.php.


Member Function Documentation

static GyroDate::add_days ( date,
days  
) [static]

Static.

Adds the number of months to given date

Parameters:
int  Timestamp
int  Number of Days
Returns:
int Timestamp

Definition at line 287 of file date.cls.php.

00287                                                       {
00288                 if ($days === 0) {
00289                         return $date;
00290                 }
00291                 
00292                 $newDate = strtotime("+$days days", $date);
00293                 return $newDate;
00294         }
static GyroDate::add_months ( date,
months  
) [static]

Static.

Adds the number of months to given date

Parameters:
int  Timestamp
int  Number of Months
Returns:
int Timestamp

Definition at line 204 of file date.cls.php.

00204                                                           {
00205                 if ($months === 0) {
00206                         return $date;
00207                 }
00208 
00209                 $newDate = strtotime("+$months months", $date);
00210                 $arrDate = getdate($date);
00211                 $arrNewDate = getdate($newDate);
00212                 $monNew = $arrNewDate["mon"] + 12 * ($arrNewDate["year"] - $arrDate["year"]);
00213                 $mon = $arrDate["mon"];
00214 
00215                 if ($monNew > $mon + $months) {
00216                         // ups, we added more then one month
00217                         // Make last day of next month
00218                         $newDate = mktime(
00219                                 $arrDate["hours"], 
00220                                 $arrDate["minutes"], 
00221                                 $arrDate["seconds"],
00222                                 $mon + $months + 1, 
00223                                 0, 
00224                                 $arrDate["year"]
00225                         );
00226                 }
00227                 return $newDate;
00228         }
static GyroDate::add_workdays ( date,
days_to_add  
) [static]

Adds work days to given date.

E.g. 27th February, 2008 is a Wednesday. If you add 5 workdays, you get Wednesday, March 5th.

You may also pass negative days, which will substract workdays. If you pass 0 for $days_to_add, $date will be forced to be a workday, which is: Saturday and Sunday will be turned into Monday, but all other days will be kept untouched

Parameters:
date  $date
int  $days_to_add
Returns:
date

Definition at line 482 of file date.cls.php.

00482                                                                  {
00483                 $absdays = abs($days_to_add);
00484                 $sign = ($absdays == $days_to_add) ? 1 : -1; 
00485                 $one_day = $sign * self::ONE_DAY;
00486                 while (!self::is_workday($date)) {
00487                         $date += $one_day;
00488                 }               
00489                 for ($i = 0; $i < $absdays; $i++) {
00490                         // Add a day
00491                         $date += $one_day;
00492                         while (!self::is_workday($date)) {
00493                                 $date += $one_day;
00494                         }       
00495                 }
00496                 return $date;
00497         }       
static GyroDate::convert_to_days ( timestamp  )  [static]

Returns days since 1.1.2000.

Definition at line 316 of file date.cls.php.

00316                                                            {
00317                 $arr_date = getdate($timestamp);
00318                 
00319                 $ret = ($arr_date['year'] - 2000) * 365;
00320                 $ret += $arr_date['yday'];
00321                 
00322                 return $ret; 
00323         }
static GyroDate::datetime ( string  )  [static]

Static.

Converts string retrieved from PHP to date

Parameters:
String  Anything that possible can be interpreted as a date
Returns:
date

Definition at line 55 of file date.cls.php.

00055                                                  {
00056                 if (is_int($string)) {
00057                         return $string;
00058                 }
00059                 
00060                 $time = false;
00061                 if(empty($string)) {
00062                         // use "now":
00063                         $time = time();
00064                 }
00065                 elseif (preg_match('/^\d{14}$/', $string)) {
00066                         // it is mysql timestamp format of YYYYMMDDHHMMSS?
00067                         $time = mktime(
00068                                 substr($string, 8, 2),
00069                                 substr($string, 10, 2),
00070                                 substr($string, 12, 2),
00071                                 substr($string, 4, 2),
00072                                 substr($string, 6, 2),
00073                                 substr($string, 0, 4)
00074                         );
00075 
00076                 }
00077                 elseif (is_numeric($string)) {
00078                         // it is a numeric string, we handle it as timestamp
00079                         $time = (int)$string;
00080                 }
00081                 else {
00082                         // strtotime should handle it
00083                         $time = strtotime($string);
00084                         if ($time == -1 || $time === false) {
00085                                 // strtotime() was not able to parse $string, use "now":
00086                                 $time = false;
00087                         }
00088                 }
00089                 return $time;
00090         }
static GyroDate::day ( date  )  [static]

Casts date to day (That is 0:00:00).

Parameters:
date  $date
Returns:
date

Definition at line 331 of file date.cls.php.

00331                                           {
00332                 return self::set_time(GyroDate::datetime($date), 0);
00333         }
static GyroDate::get_day ( date  )  [static]

Returns day of given date.

Parameters:
date  $date
Returns:
int 0 on error

Definition at line 379 of file date.cls.php.

00379                                               {
00380                 return intval(date('j', $date));
00381         }
static GyroDate::get_month ( date  )  [static]

Returns number of month of given date.

1 = January, 2 = February etc

Parameters:
date  $date
Returns:
int 0 on error

Definition at line 391 of file date.cls.php.

00391                                                 {
00392                 return intval(date('n', $date));
00393         }
static GyroDate::get_week ( date  )  [static]

Return week in year of given Date.

Parameters:
date  $date
Returns:
int

Definition at line 369 of file date.cls.php.

00369                                                {
00370                 return Cast::int(date('W', self::datetime($date)));
00371         }
static GyroDate::get_weekday ( date  )  [static]

Return weekday of given Date.

Parameters:
date  $date
Returns:
int 0 = Sunday, 1 = Monday, ..., 6 = Saturday

Definition at line 358 of file date.cls.php.

00358                                                   {
00359                 $arr_date = getdate(self::datetime($date));
00360                 return $arr_date['wday'];
00361         }
static GyroDate::get_year ( date  )  [static]

Returns year of given date.

Parameters:
date  $date
Returns:
int 0 on error

Definition at line 401 of file date.cls.php.

00401                                                {
00402                 return intval(date('Y', $date));
00403         }
static GyroDate::http_date ( date  )  [static]

Converts timestamp to string used in HTTP-Header fields (such as "Expires").

Parameters:
timestamp  $date
Returns:
string

Definition at line 168 of file date.cls.php.

00168                                                 {
00169                 return gmdate('D, d M Y H:i:s \G\M\T', $date);
00170         }
static GyroDate::is_holiday ( date  )  [static]

Returns true if given date is a holiday.

Attention: This method will not necessarily recognize Sundays as holidays! It only compares given date with self::$holidays.

Parameters:
date  $date
Returns:
bool

Definition at line 414 of file date.cls.php.

00414                                                  {
00415                 $date = self::day($date);
00416                 // Check date for holidays
00417                 foreach(self::$holidays as $holiday) {
00418                         $holiday = self::day($holiday);
00419                         if ($date == $holiday) {
00420                                 return true; 
00421                         }
00422                 }
00423                 return false;                           
00424         }
static GyroDate::is_this_month ( time  )  [static]

Returns true if the given datetime is of this month.

Definition at line 509 of file date.cls.php.

00509                                                     {
00510                 $arr_time = getdate(self::datetime($time));
00511                 $arr_now = getdate(time());
00512                 return ($arr_now['year'] == $arr_time['year'] && $arr_now['mon'] == $arr_time['mon']);  
00513         }
static GyroDate::is_this_year ( time  )  [static]

Returns true if the given datetime is of this year.

Definition at line 518 of file date.cls.php.

00518                                                    {
00519                 $arr_time = getdate(self::datetime($time));
00520                 $arr_now = getdate(time());
00521                 return ($arr_now['year'] == $arr_time['year']);                 
00522         }
static GyroDate::is_today ( time  )  [static]

Returns true if the given datetime is of today.

Definition at line 502 of file date.cls.php.

00502                                                {
00503                 return self::day($time) == self::day(time()); 
00504         }
static GyroDate::is_workday ( date  )  [static]

Returns if given date is a work day (that is: not saturday or sunday).

Parameters:
date  $date
Returns:
bool

Definition at line 432 of file date.cls.php.

00432                                                  {
00433                 $date = self::day($date);
00434                 // Check weekday
00435                 if (in_array(self::get_weekday($date), self::$non_workdays)) {
00436                         return false;                   
00437                 }
00438                 // Check date for holidays
00439                 return !self::is_holiday($date);
00440         }
static GyroDate::iso_date ( date  )  [static]

Static.

Converts timestamp to ISO DateTime string

Parameters:
int  Timestamp
Returns:
string

Definition at line 148 of file date.cls.php.

00148                                                {
00149                 return date('c', $date);
00150         }
static GyroDate::local_date ( date,
includetime = true  
) [static]

Static.

Converts timestamp to DateTime string that respects locale settings

Parameters:
int  Timestamp
bool  $includetime True to include time
Returns:
string

Definition at line 179 of file date.cls.php.

00179                                                                       {
00180                 $format = '';
00181                 switch (self::$local_date_order) {
00182                         case self::DAY_MONTH_YEAR:
00183                                 $format = ($includetime) ? 'j.n.Y, G:i:s' : 'j.n.Y';
00184                                 break;
00185                         case self::MONTH_DAY_YEAR:
00186                                 $format = ($includetime) ? 'n/j/Y, G:i:s' : 'n/j/Y';
00187                                 break;
00188                         case self::YEAR_MONTH_DAY:
00189                         default:
00190                                 $format = ($includetime) ? 'Y-m-d, H:i:s' : 'Y-m-d';
00191                                 break;
00192                 }
00193                 return date($format, $date);
00194         }
static GyroDate::month ( date  )  [static]

Casts date to month (That is 1st, 0:00:00).

Parameters:
date  $date
Returns:
date

Definition at line 345 of file date.cls.php.

00345                                             {
00346                 $ret = GyroDate::datetime($date);
00347                 $ret = self::set_time($ret, 0);
00348                 $ret = self::set_day($ret, 1);
00349                 return $ret;            
00350         }       
static GyroDate::mysql_date ( date,
includetime = true  
) [static]

Static.

Converts timestamp to MYSQL Data

Parameters:
int  Timestamp
Returns:
string

Definition at line 124 of file date.cls.php.

00124                                                                       {
00125                 if ($includetime) {
00126                         return date('Y-m-d H:i:s', $date);
00127                 } else {
00128                         return date('Y-m-d', $date);
00129                 }
00130         }
static GyroDate::mysql_time ( date  )  [static]

Static.

Converts timestamp to MYSQL Time

Parameters:
int  Timestamp
Returns:
string

Definition at line 138 of file date.cls.php.

00138                                                  {
00139                 return date('H:i:s', $date);
00140         }
static GyroDate::parse ( date,
format  
) [static]

Calls strptime, but returns unix timestamp.

Usually it should be enough to call GyroDate::datetime(), but if you deal with unusual date and time string of known format, use this function.

Parameters:
string  $date The datetime as a string
string  $format See strptime for format description
Returns:
mixed Timestamp or FALSE

Definition at line 102 of file date.cls.php.

00102                                                      {
00103                 $ret = false;
00104                 $dt_arr = strptime($date, $format);
00105                 if (is_array($dt_arr)) {
00106                         $ret = mktime(
00107                                 $dt_arr['tm_hour'],
00108                                 $dt_arr['tm_min'],
00109                                 $dt_arr['tm_sec'],
00110                                 $dt_arr['tm_mon'] + 1,
00111                                 $dt_arr['tm_mday'],
00112                                 $dt_arr['tm_year'] + 1900       
00113                         );
00114                 }
00115                 return $ret;
00116         }       
static GyroDate::rfc_date ( date  )  [static]

Static.

Converts timestamp to RFC 2822 DateTime string

Parameters:
int  Timestamp
Returns:
string

Definition at line 158 of file date.cls.php.

00158                                                {
00159                 return date('r', $date);
00160         }
static GyroDate::set_day ( date,
day  
) [static]

Sets day on given date (keeps date).

Parameters:
date  $date
int  $day
Returns:
date

Definition at line 463 of file date.cls.php.

00463                                                     {
00464                 $arr_date = getdate($date);
00465                 return mktime($arr_date['hours'], $arr_date['minutes'], $arr_date['seconds'], $arr_date['mon'], $day, $arr_date['year']);
00466         }
static GyroDate::set_time ( date,
hour,
min = 0,
sec = 0  
) [static]

Sets Time on given date (keeps date).

Parameters:
date  $date
int  $hour
int  $min
int  $sec
Returns:
date

Definition at line 451 of file date.cls.php.

00451                                                                           {
00452                 $arr_date = getdate($date);
00453                 return mktime($hour, $min, $sec, $arr_date['mon'], $arr_date['mday'], $arr_date['year']);
00454         }
static GyroDate::substract_days ( date,
days  
) [static]

Static.

Substracts the number of days from given date

Parameters:
int  Timestamp
int  Number of days
Returns:
int Timestamp

Definition at line 304 of file date.cls.php.

00304                                                             {
00305                 if ($days === 0) {
00306                         return $date;
00307                 }
00308 
00309                 $newDate = strtotime("-$days days", $date);
00310                 return $newDate;
00311         }
static GyroDate::substract_months ( date,
months  
) [static]

Static.

Substracts the number of months to given date

Parameters:
int  Timestamp
int  Number of Months
Returns:
int Timestamp

Definition at line 238 of file date.cls.php.

00238                                                                 {
00239                 if ($months === 0) {
00240                         return $date;
00241                 }
00242 
00243                 $newDate = strtotime("-$months months", $date);
00244                 $arrDate = getdate($date);
00245                 $arrNewDate = getdate($newDate);
00246                 //print_r($arrDate);
00247                 //print_r($arrNewDate);
00248                 //die();
00249                 $monNew = $arrNewDate["mon"] - 12 * ($arrDate["year"] - $arrNewDate["year"]);
00250                 $mon = $arrDate["mon"];
00251 
00252                 if ($monNew < $mon - $months) {
00253                         // ups, we substracted more then one month
00254                         // Make first day of next month
00255                         $newDate = mktime(
00256                                 $arrDate["hours"], 
00257                                 $arrDate["minutes"], 
00258                                 $arrDate["seconds"],
00259                                 $mon - $months + 1, 
00260                                 1, 
00261                                 $arrDate["year"]
00262                         );
00263                 }
00264                 else if ($monNew > $mon - $months) {
00265                         // ups, we substracted less then one month
00266                         // Make last day of next month
00267                         $newDate = mktime(
00268                                 $arrDate["hours"], 
00269                                 $arrDate["minutes"], 
00270                                 $arrDate["seconds"],
00271                                 $mon - $months + 1, 
00272                                 0, 
00273                                 $arrDate["year"]
00274                         );
00275                 }
00276                 return $newDate;
00277         }
static GyroDate::today (  )  [static]

Definition at line 335 of file date.cls.php.

00335                                    {
00336         return self::day(time());
00337     }

Member Data Documentation

GyroDate::$holidays = array() [static]

Definition at line 41 of file date.cls.php.

GyroDate::$local_date_order = self::MONTH_DAY_YEAR [static]

Definition at line 47 of file date.cls.php.

GyroDate::$non_workdays = array(self::SUNDAY, self::SATURDAY) [static]

Definition at line 35 of file date.cls.php.

const GyroDate::DAY_MONTH_YEAR = 'DMY'

Definition at line 26 of file date.cls.php.

const GyroDate::FRIDAY = 5

Definition at line 22 of file date.cls.php.

const GyroDate::MONDAY = 1

Definition at line 18 of file date.cls.php.

const GyroDate::MONTH_DAY_YEAR = 'MDY'

Definition at line 27 of file date.cls.php.

const GyroDate::ONE_DAY = 86400

Definition at line 11 of file date.cls.php.

const GyroDate::ONE_HOUR = 3600

Definition at line 10 of file date.cls.php.

Definition at line 9 of file date.cls.php.

const GyroDate::ONE_MONTH = 2592000

30 days

Definition at line 14 of file date.cls.php.

const GyroDate::ONE_WEEK = 604800

Definition at line 12 of file date.cls.php.

const GyroDate::ONE_YEAR = 31536000

365 days

Definition at line 16 of file date.cls.php.

const GyroDate::SATURDAY = 6

Definition at line 23 of file date.cls.php.

const GyroDate::SUNDAY = 0

Definition at line 24 of file date.cls.php.

const GyroDate::THURSDAY = 4

Definition at line 21 of file date.cls.php.

const GyroDate::TUESDAY = 2

Definition at line 19 of file date.cls.php.

Definition at line 20 of file date.cls.php.

const GyroDate::YEAR_MONTH_DAY = 'YMD'

Definition at line 28 of file date.cls.php.


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