# DateUtils.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2007-2009 by Ivan Y. Khvostishkov                       *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00017     final class DateUtils extends StaticFactory
    00018     {
    00019         public static function getAgeByBirthDate(
    00020             Date $birthDate, /* Date*/ $actualDate = null
    00021         )
    00022         {
    00023             if ($actualDate)
    00024                 Assert::isInstance($actualDate, 'Date');
    00025             else
    00026                 $actualDate = Date::makeToday();
    00027
    00028             $result = $actualDate->getYear() - $birthDate->getYear();
    00029
    00030             if (
    00031                 $actualDate->getMonth() < $birthDate->getMonth()
    00032                 || (
    00033                     $actualDate->getMonth() == $birthDate->getMonth()
    00034                     && $actualDate->getDay() < $birthDate->getDay()
    00035                 )
    00036             ) {
    00037                     // - Happy birthday?
    00038                     // - Happy go to hell. Not yet in this year.
    00039                     --$result;
    00040             }
    00041
    00042             return $result;
    00043         }
    00044
    00045         public static function makeFirstDayOfMonth(Date $date)
    00046         {
    00047             return
    00048                 Timestamp::create(
    00049                     mktime(0, 0, 0, $date->getMonth(), 1, $date->getYear())
    00050                 );
    00051         }
    00052
    00053         public static function makeLastDayOfMonth(Date $date)
    00054         {
    00055             return
    00056                 Timestamp::create(
    00057                     mktime(0, 0, 0, $date->getMonth() + 1, 0, $date->getYear())
    00058                 );
    00059         }
    00060
    00061         public static function makeDatesListByRange(
    00062             DateRange $range, IntervalUnit $unit, $hash = true
    00063         )
    00064         {
    00065             $date = $unit->truncate($range->getStart());
    00066
    00067             if ('Date' == get_class($range->getStart()))
    00068                 $date = Date::create($date->toStamp());
    00069
    00070             $dates = array();
    00071
    00072             do {
    00073                 if ($hash)
    00074                     $dates[$date->toString()] = $date;
    00075                 else
    00076                     $dates[] = $date;
    00077
    00078                 $date = $date->spawn('+ 1'.$unit->getName());
    00079             } while (
    00080                 $range->getEnd()->toStamp() >= $date->toStamp()
    00081             );
    00082
    00083             return $dates;
    00084         }
    00085
    00089         public static function alignToSeconds(Timestamp $stamp, $seconds)
    00090         {
    00091             $rawStamp = $stamp->toStamp();
    00092
    00093             $align = floor($rawStamp / $seconds);
    00094
    00095             return Timestamp::create($align * $seconds);
    00096         }
    00097     }
    00098 ?>