Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file DateUtil.class.php

Documentation is available at DateUtil.class.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright Robert Gasch
  6.  * @link http://www.zikula.org
  7.  * @version $Id: DateUtil.class.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @author Robert Gasch rgasch@gmail.com
  10.  * @uses generic data utililty class
  11.  * @package Zikula_Core
  12.  */
  13.  
  14. /**
  15.  * constants for this class
  16.  */
  17. define ('DATEFORMAT_FIXED''%Y-%m-%d %H:%M:%S');
  18.  
  19. /**
  20.  * DateUtil
  21.  *
  22.  * @package Zikula_Core
  23.  * @subpackage DateUtil
  24.  */
  25. class DateUtil
  26. {
  27.     /**
  28.      * Return a formatted datetime for the given timestamp (or for now)
  29.      *
  30.      * @param time      The timestamp (string) which we wish to format (default==now)
  31.      * @param format    The format to use when formatting the date (optional)
  32.      *
  33.      * @return datetime The datetime formatted according to the specified format
  34.      */
  35.     function getDatetime ($time=''$format=DATEFORMAT_FIXED)
  36.     {
  37.         switch(trim(strtolower($format))){
  38.             case 'datelong':
  39.                 $format _DATELONG;
  40.                 break;
  41.             case 'datebrief':
  42.                 $format _DATEBRIEF;
  43.                 break;
  44.             case 'datestring':
  45.                 $format _DATESTRING;
  46.                 break;
  47.             case 'datestring2':
  48.                 $format _DATESTRING2;
  49.                 break;
  50.             case 'datetimebrief':
  51.                 $format _DATETIMEBRIEF;
  52.                 break;
  53.             case 'datetimelong':
  54.                 $format _DATETIMELONG;
  55.                 break;
  56.             case 'timebrief':
  57.                 $format _TIMEBRIEF;
  58.                 break;
  59.             case 'timelong':
  60.                 $format _TIMELONG;
  61.                 break;
  62.             default:
  63.         // switch
  64.  
  65.         // bp 2008-03-29 - translation of day and month names if Windows utf-8 system is used
  66.         if ($time{
  67.             $dtstr strftime($format$time);
  68.         else {
  69.             $dtstr strftime($format);
  70.         }
  71.         $encoding pnSessionGetVar('encoding');
  72.         $cs strtolower(_CHARSET);
  73.         $cs == 'utf-8' $cs 'utf8' '';    
  74.         // applies only if $encoding is set, iconv is available and we're using utf8
  75.         if ((!empty($encoding)) && ($cs == 'utf8')) {
  76.             // check for iconv - not included in std. win distribution
  77.             if (!extension_loaded('iconv')) {
  78.                 if (!dl('iconv.so')) {
  79.                     return $dtstr;
  80.                 }
  81.             }    
  82.             if ($encoding !=  $cs{
  83.                 $dtstr iconv($encoding$cs$dtstr);
  84.             }
  85.         }
  86.         return $dtstr;
  87.     }
  88.  
  89.  
  90.     /**
  91.      * Reformat a given datetime according to the specified format
  92.      *
  93.      * @param datetime    The (string) datetime to reformat
  94.      * @param format      The format to use when formatting the date (optional)
  95.      *
  96.      * @return datetime The datetime formatted according to the specified format
  97.      */
  98.     function formatDatetime ($datetime=null$format=DATEFORMAT_FIXED)
  99.     {
  100.         if ($datetime === null{
  101.             return '';
  102.         }
  103.  
  104.         if (!$datetime{
  105.             return strftime($format);
  106.         }
  107.         
  108.         $dTime DateUtil::makeTimestamp($datetime);
  109.         return DateUtil::getDatetime ($dTime$format);
  110.     }
  111.  
  112.  
  113.     /**
  114.      * Build a datetime string from the supplied fields
  115.      *
  116.      * @param year      The year
  117.      * @param month     The month
  118.      * @param day       The day
  119.      * @param hour      The hour (optional) (default==0)
  120.      * @param minute    The minute (optional) (default==0)
  121.      * @param second    The second (optional) (default==0)
  122.      * @param format    The format to use when formatting the date (optional)
  123.      *
  124.      * @return datetime The datetime formatted according to the specified format
  125.      */
  126.     function buildDatetime ($year$month$day$hour=0$minute=0$second=0$format=DATEFORMAT_FIXED)
  127.     {
  128.         $dTime mktime ($hour$minute$second$month$day$year);
  129.         return strftime($format$dTime);
  130.     }
  131.  
  132.  
  133.     /**
  134.      * Return a formatted datetime at the end of the business day n days from now
  135.      *
  136.      * @param days      The number of days to advance (optional) (detault==1)
  137.      * @param format    The format to use when formatting the date (optional)
  138.      * @param hour      The hour of the target time to set (optional) (default=null, means params is taken from now)
  139.      * @param minute    The minute of the target time to set (optional) (default=null, means params is taken from now)
  140.      * @param second    The second of the target time to set (optional) (default=null, means params is taken from now)
  141.      *
  142.      * @return datetime The datetime formatted according to the specified format
  143.      */
  144.     function getDatetime_NextDay ($num=1$format=DATEFORMAT_FIXED$year=null$month=null$day=null$hour=null$minute=null$second=null)
  145.     {
  146.         $next mktime ($hour!=null   ? (int)$hour     date('H')
  147.                         $minute!=null ? (int)$minute   date('i')
  148.                         $second!=null ? (int)$second   date('s')
  149.                         $month!=null  ? (int)$month    date('m')
  150.                         $day!=null    ? (int)$day+$num date('d')+$num
  151.                         $year!=null   ? (int)$year     date('y'));
  152.         return strftime($format$next);
  153.     }
  154.  
  155.  
  156.     /**
  157.      * Return a formatted datetime at the end of the business day n week from now
  158.      *
  159.      * @param num       The number of weeks to advance (optional) (detault==1)
  160.      * @param format    The format to use when formatting the date (optional)
  161.      * @param year      The year of the target time to set (optional) (default=null, means param is taken from now)
  162.      * @param month     The month of the target time to set (optional) (default=null, means param is taken from now)
  163.      * @param day       The day of the target time to set (optional) (default=null, means param is taken from now)
  164.      * @param hour      The hour of the target time to set (optional) (default=null, means param is taken from now)
  165.      * @param minute    The minute of the target time to set (optional) (default=null, means param is taken from now)
  166.      * @param second    The second of the target time to set (optional) (default=null, means param is taken from now)
  167.      *
  168.      * @return datetime The datetime formatted according to the specified format
  169.      */
  170.     function getDatetime_NextWeek ($num=1$format=DATEFORMAT_FIXED$year=null$month=null$day=null$hour=null$minute=null$second=null)
  171.     {
  172.         $num *= 7;
  173.         $next mktime ($hour!=null   ? (int)$hour     date('H')
  174.                         $minute!=null ? (int)$minute   date('i')
  175.                         $second!=null ? (int)$second   date('s')
  176.                         $month!=null  ? (int)$month    date('m')
  177.                         $day!=null    ? (int)$day+$num date('d')+$num
  178.                         $year!=null   ? (int)$year     date('y'));
  179.         return strftime($format$next);
  180.     }
  181.  
  182.  
  183.     /**
  184.      * Return a formatted datetime at the end of the business day n months from now
  185.      *
  186.      * @param num       The number of months to advance (optional) (default=1)
  187.      * @param format    The format to use when formatting the date (optional)
  188.      * @param year      The year of the target time to set (optional) (default=null, means param is taken from now)
  189.      * @param month     The month of the target time to set (optional) (default=null, means param is taken from now)
  190.      * @param day       The day of the target time to set (optional) (default=null, means param is taken from now)
  191.      * @param hour      The hour of the target time to set (optional) (default=null, means params is taken from now)
  192.      * @param minute    The minute of the target time to set (optional) (default=null, means params is taken from now)
  193.      * @param second    The second of the target time to set (optional) (default=null, means params is taken from now)
  194.      *
  195.      * @return datetime The datetime formatted according to the specified format
  196.      */
  197.     function getDatetime_NextMonth ($num=1$format=DATEFORMAT_FIXED$year=null$month=null$day=null$hour=null$minute=null$second=null)
  198.     {
  199.         $next mktime ($hour!=null   ? (int)$hour     date('H')
  200.                         $minute!=null ? (int)$minute   date('i')
  201.                         $second!=null ? (int)$second   date('s')
  202.                         $month!=null  ? (int)$month    date('m')+$num
  203.                         $day!=null    ? (int)$day+$num date('d'),
  204.                         $year!=null   ? (int)$year     date('y'));
  205.         return strftime($format$next);
  206.     }
  207.  
  208.  
  209.     /**
  210.      * Return a formatted datetime at the end of the business day n years from now
  211.      *
  212.      * @param num       The number of years to advance (optional) (default=1)
  213.      * @param format    The format to use when formatting the date (optional)
  214.      * @param year      The year of the target time to set (optional) (default=null, means param is taken from now)
  215.      * @param month     The month of the target time to set (optional) (default=null, means param is taken from now)
  216.      * @param day       The day of the target time to set (optional) (default=null, means param is taken from now)
  217.      * @param hour      The hour of the target time to set (optional) (default=null, means params is taken from now)
  218.      * @param minute    The minute of the target time to set (optional) (default=null, means params is taken from now)
  219.      * @param second    The second of the target time to set (optional) (default=null, means params is taken from now)
  220.      *
  221.      * @return datetime The datetime formatted according to the specified format
  222.      */
  223.     function getDatetime_NextYear ($num=1$format=DATEFORMAT_FIXED$year=null$month=null$day=null$hour=null$minute=null$second=null)
  224.     {
  225.         $next mktime ($hour!=null   ? (int)$hour     date('H')
  226.                         $minute!=null ? (int)$minute   date('i')
  227.                         $second!=null ? (int)$second   date('s')
  228.                         $month!=null  ? (int)$month    date('m')
  229.                         $day!=null    ? (int)$day+$num date('d'),
  230.                         $year!=null   ? (int)$year     date('y')+$num);
  231.         return strftime($format$next);
  232.     }
  233.  
  234.  
  235.     /**
  236.      * Return the date portion of a datetime timestamp
  237.      *
  238.      * @param datetime  The date to parse (optional) (default=='', reverts to now)
  239.      * @param format    The format to use when formatting the date (optional)
  240.  
  241.      * @return string   The Date portion of the specified datetime
  242.      */
  243.     function getDatetime_Date ($datetime=''$format=DATEFORMAT_FIXED)
  244.     {
  245.         if (!$datetime{
  246.             $datetime DateUtil::getDatetime ();
  247.         }
  248.  
  249.         $dTime strtotime($datetime);
  250.         $sTime DateUtil::getDatetime ($dTime$format);
  251.  
  252.         if ($format == DATEFORMAT_FIXED{
  253.             return substr ($sTime08);
  254.         }
  255.  
  256.         $spaceOffset strpos ($datetime' ');
  257.         return substr ($sTime0$spaceOffset);
  258.     }
  259.  
  260.  
  261.     /**
  262.      * Return the time portion of a datetime timestamp
  263.      *
  264.      * @param datetime  The date to parse (optional) (default=='', reverts to now)
  265.      * @param format    The format to use when formatting the date (optional)
  266.      *
  267.      * @return string   The Time portion of the specified datetime
  268.      */
  269.     function getDatetime_Time ($datetime=''$format=DATEFORMAT_FIXED)
  270.     {
  271.         if (!$datetime{
  272.             $datetime DateUtil::getDatetime ();
  273.         }
  274.  
  275.         $dTime strtotime($datetime);
  276.         $sTime DateUtil::getDatetime ($dTime$format);
  277.  
  278.         if ($format == DATEFORMAT_FIXED{
  279.             return substr ($sTime9);
  280.         }
  281.  
  282.         $spaceOffset strpos ($datetime' ');
  283.         return substr ($datetime$spaceOffset+1);
  284.     }
  285.  
  286.  
  287.     /**
  288.      * Return the requested field from the supplied date
  289.      *
  290.      * Since the date fields can change depending on the date format,
  291.      * the following convention is used when referring to date fields:<br />
  292.      *   Field 1    ->     Year<br />
  293.      *   Field 2    ->     Month<br />
  294.      *   Field 3    ->     Day<br />
  295.      *   Field 4    ->     Hour<br />
  296.      *   Field 5    ->     Minute<br />
  297.      *   Field 6    ->     Second<br />
  298.      *
  299.      *
  300.      * @param datetime  The field number to return
  301.      * @param field     The date to parse (default=='', reverts to now)
  302.      *
  303.      * @return string   The requested datetime field
  304.      */
  305.     function getDatetime_Field ($datetime$field)
  306.     {
  307.         if (!$datetime{
  308.             $datetime DateUtil::getDatetime ();
  309.         }
  310.  
  311.         $dTime strtotime($datetime);
  312.         $sTime DateUtil::getDatetime ($dTime);
  313.  
  314.         $field--;         // adjust for human counting
  315.  
  316.         if ($field <= 2)  // looking for a date part
  317.         {
  318.             $date DateUtil::getDatetime_Date ($datetime);
  319.             $fields explode ('-'$date);
  320.             return $fields[$field];
  321.         }
  322.         else              // looking for a time part
  323.         {
  324.             $field -= 3;
  325.             $time DateUtil::getDatetime_Time ($datetime);
  326.             $fields explode (':'$time);
  327.             return $fields[$field];
  328.         }
  329.     }
  330.  
  331.  
  332.     /**
  333.      * Return an structured array holding the differences between 2 dates.
  334.      *
  335.      * The returned array will be structured as follows:<br>
  336.      *   Array (<br />
  337.      *          [d] => _numeric_day_value_<br />
  338.      *          [h] => _numeric_hour_value_<br />
  339.      *          [m] => _numeric_minute_value_<br />
  340.      *          [s] => _numeric_second_value_ )<br />
  341.      *
  342.      * @param date1     The first date
  343.      * @param date2     The second date
  344.      *
  345.      * @return array    The structured array containing the datetime difference
  346.      */
  347.     function getDatetimeDiff ($date1$date2)
  348.     {
  349.         $s strtotime($date2strtotime($date1);
  350.  
  351.         $d intval($s/86400);
  352.         $s -= $d*86400;
  353.         $h intval($s/3600);
  354.         $s -= $h*3600;
  355.         $m intval($s/60);
  356.         $s -= $m*60;
  357.  
  358.         return array("d"=>$d,"h"=>$h,"m"=>$m,"s"=>$s);
  359.     }
  360.  
  361.  
  362.     /**
  363.      * Return an field holding the differences between 2 dates expressed
  364.      * in units of the field requested
  365.      *
  366.      * Since the date fields can change depending on the date format,
  367.      * the following convention is used when referring to date fields:<br />
  368.      *   Field 1    ->     Year<br />
  369.      *   Field 2    ->     Month<br />
  370.      *   Field 3    ->     Day<br />
  371.      *   Field 4    ->     Hour<br />
  372.      *   Field 5    ->     Minute<br />
  373.      *   Field 6    ->     Second<br />
  374.      *
  375.      * @param date1     The first date
  376.      * @param date2     The second date
  377.      * @param field     The field (unit) in which we want the different
  378.      *
  379.      * @return float The difference in units of the specified field
  380.      */
  381.     function getDatetimeDiff_AsField ($date1$date2$field)
  382.     {
  383.         $s    strtotime($date2strtotime($date1);
  384.         $diff 0;
  385.  
  386.         if ($field == 1{
  387.             $diff $s/(60*60*24*31*12);
  388.         }
  389.         else
  390.         if ($field == 2{
  391.             $diff $s/(60*60*24*31);
  392.         }
  393.         else
  394.         if ($field == 3{
  395.             $diff $s/(60*60*24);
  396.         }
  397.         else
  398.         if ($field == 4{
  399.             $diff $s/(60*60);
  400.         }
  401.         else
  402.         if ($field == 5{
  403.             $diff $s/(60);
  404.         }
  405.         else
  406.         {
  407.             $diff $s;
  408.         }
  409.  
  410.         return $diff;
  411.     }
  412.  
  413.  
  414.     /**
  415.      * Calculate day-x of KW in a YEAR
  416.      * @param day       0 for monday, 6 for sunday,....
  417.      * @param kw        week of the year
  418.      * @param year      year
  419.      * @param flag      u or s (unixtimestamp or MySQLDate)
  420.      *
  421.      * @return unixtimestamp or sqlDate
  422.      */
  423.     function getDateofKW ($day$kw$year$flag='s')
  424.     {
  425.         $wday date('w',mktime(0,0,0,1,1,$year))// 1=Monday,...,7 = Sunday
  426.  
  427.         if ($wday <= 4{
  428.             $firstday mktime(0,0,0,1,1-($wday-1)+$day,$year);
  429.         else {
  430.             $firstday mktime(0,0,0,1,1+(7-$wday+1)+$day,$year);
  431.         }
  432.  
  433.         $month date('m',$firstday);
  434.         $year  date('Y',$firstday);
  435.         $day   date('d',$firstday);
  436.  
  437.         $adddays ($kw-1)*7;
  438.  
  439.         if ($flag != 's'{
  440.             $return mktime(0,0,0,$month,$day+$adddays,$year);
  441.         else {
  442.             $return DateUtil::getDatetime(mktime(0,0,0,$month$day+$adddays$year));
  443.         }
  444.  
  445.         return $return;
  446.     }
  447.  
  448.  
  449.     /**
  450.      * Return a the number of days in the given month/year
  451.      *
  452.      * @param month     The (human) month number to check
  453.      * @param year      The year number to check
  454.      *
  455.      * @return integer  The number of days in the given month/year
  456.      */
  457.     function getDaysInMonth ($month$year)
  458.     {
  459.         if ($month || $month 12{
  460.             return 0;
  461.         }
  462.  
  463.         $days array(312831303130313130313031);
  464.  
  465.         $d $days[$month-1];
  466.  
  467.         if ($month == 2{
  468.             // Check for leap year, no 4000 rule
  469.             if ($year%== 0{
  470.                 if ($year%100 == 0{
  471.                     if ($year%400 == 0{
  472.                         $d 29;
  473.                     }
  474.                 else {
  475.                     $d 29;
  476.                 }
  477.             }
  478.         }
  479.  
  480.         return $d;
  481.     }
  482.  
  483.  
  484.     /**
  485.      * Return an array of weekdays for the given month
  486.      *
  487.      * @param month     The (human) month number to check
  488.      * @param year      The year number to check
  489.      *
  490.      * @return integer  The number of days in the given month/year
  491.      */
  492.     function getWeekdaysInMonth ($month$year)
  493.     {
  494.         $nDays DateUtil::getDaysInMonth ($month$year);
  495.  
  496.         $weekdays array();
  497.         for ($i=1$i<=$nDays$i++{
  498.             $time mktime (1200$month$i$year);
  499.             $tDate getdate ($time);
  500.             $weekdays[$i$tDate['wday'];
  501.         }
  502.  
  503.         return $weekdays;
  504.     }
  505.  
  506.  
  507.     /**
  508.      * Return an array of dates for the given month
  509.      *
  510.      * @param month     The (human) month number to check
  511.      * @param year      The year number to check
  512.      *
  513.      * @return integer  The number of days in the given month/year
  514.      */
  515.     function getMonthDates ($month$year)
  516.     {
  517.         $dates array();
  518.         $days DateUtil::getDaysInMonth ($month$year);
  519.  
  520.         for ($i=1$i<=$days$i++{
  521.             $dates[$iDateUtil::buildDatetime ($year$month$i);
  522.         }
  523.  
  524.         return $dates;
  525.     }
  526.  
  527.  
  528.     /**
  529.      * Parses a user interface date string (excluding time) into a timestamp
  530.      *
  531.      * Currently only the format YYYY-MM-DD is supported, but future versions
  532.      * will be able to parse more diverse formats.
  533.      *
  534.      * @param text      The UI date string
  535.      *
  536.      * @return date     The timestamp or null in case of errors
  537.      */
  538.     function parseUIDate($text)
  539.     {
  540.         $elements split('-'$text);
  541.         if (count($elements== 3)
  542.         {
  543.             list ($year,$month,$day$elements;
  544.         }
  545.         else
  546.         {
  547.             $year $month $day 0;
  548.         }
  549.  
  550.         if (!checkdate($month,$day,$year))
  551.         {
  552.             return null;
  553.         }
  554.  
  555.         return mktime(0,0,0$month,$day,$year);
  556.     }
  557.  
  558.  
  559.     /**
  560.      * Parses a user interface date+time string into a timestamp
  561.      *
  562.      * Currently only the format YYYY-MM-DD HH:MM is supported, but future versions
  563.      * will be able to parse more diverse formats.
  564.      *
  565.      * @param text      The UI date+time string
  566.      *
  567.      * @return date     The timestamp or null in case of errors
  568.      */
  569.     function parseUIDateTime($text)
  570.     {
  571.         if (preg_match("/(\\d+)-(\\d+)-(\\d+)(\s+(\\d+)(:(\\d+))?)?/"$text$matches))
  572.         {
  573.             $year $matches[1];
  574.             $month $matches[2];
  575.             $day $matches[3];
  576.             $sec 0;
  577.  
  578.             if (count($matches>= 4)
  579.             {
  580.                 $hour $matches[5];
  581.             }
  582.             else
  583.             {
  584.                 $hour 0;
  585.             }
  586.  
  587.             if (count($matches>= 5)
  588.             {
  589.                 $min $matches[7];
  590.             }
  591.             else
  592.             {
  593.                 $min 0;
  594.             }
  595.  
  596.             if (!checkdate($month,$day,$year|| $hour>23 || $min>59)
  597.             {
  598.                 return null;
  599.             }
  600.         }
  601.         else
  602.           return null;
  603.  
  604.         return mktime($hour,$min,$sec$month,$day,$year);
  605.     }
  606.  
  607.     /**
  608.      * create a unix timestamp from either a unix timestamp (sic!), a MySQL timestamp
  609.      * or a string. This code is taken from smarty_make_timestamp.php,
  610.      * credits go to Monte Ohrt <monte at ohrt dot com>
  611.      *
  612.      * We use a copy of the code here due to performance reasons.
  613.      *
  614.      * @param int or text       a timestamp in one of the formats mentioned
  615.      *
  616.      * @return int              a unix timestamp
  617.      *
  618.      */
  619.     function makeTimestamp($string='')
  620.     {
  621.         if(empty($string)) {
  622.             // use "now":
  623.             $time time();
  624.         
  625.         elseif (preg_match('/^\d{14}$/'$string)) {
  626.             // it is mysql timestamp format of YYYYMMDDHHMMSS?            
  627.             $time mktime(substr($string82),substr($string102),substr($string122),
  628.                            substr($string42),substr($string62),substr($string04));
  629.             
  630.         elseif (is_numeric($string)) {
  631.             // it is a numeric string, we handle it as timestamp
  632.             $time = (int)$string;
  633.             
  634.         else {
  635.             // strtotime should handle it
  636.             $time strtotime($string);
  637.             if ($time == -|| $time === false{
  638.                 // strtotime() was not able to parse $string, use "now":
  639.                 $time time();
  640.             }
  641.         }
  642.         return $time;
  643.     }
  644. }

Documentation generated on Fri, 18 Jul 2008 21:44:35 +0200 by phpDocumentor 1.4.1