Ejemplo n.º 1
0
 /**
  * Returns the date the specified no of seconds from the given date
  *
  * If leap seconds are specified to be counted, the passed time must be UTC.
  * To subtract seconds use a negative value for the '$pn_seconds' parameter.
  *
  * N.B. the return type of the second part of the date is float if
  * either '$pn_seconds' or '$pn_second' is a float; otherwise, it
  * is integer.
  *
  * @param mixed $pn_seconds   seconds to add as integer or float
  * @param int   $pn_day       the day of the month
  * @param int   $pn_month     the month
  * @param int   $pn_year      the year
  * @param int   $pn_hour      the hour
  * @param int   $pn_minute    the minute
  * @param mixed $pn_second    the second as integer or float
  * @param bool  $pb_countleap whether to count leap seconds (defaults to
  *                             DATE_COUNT_LEAP_SECONDS)
  *
  * @return   array      array of year, month, day, hour, minute, second
  * @access   public
  * @static
  * @since    Method available since Release 1.5.0
  */
 function addSeconds($pn_seconds, $pn_day, $pn_month, $pn_year, $pn_hour, $pn_minute, $pn_second, $pb_countleap = DATE_COUNT_LEAP_SECONDS)
 {
     if ($pn_seconds == 0) {
         return array((int) $pn_year, (int) $pn_month, (int) $pn_day, (int) $pn_hour, (int) $pn_minute, $pn_second);
     }
     if ($pb_countleap) {
         $hn_seconds = $pn_seconds;
         $hn_day = (int) $pn_day;
         $hn_month = (int) $pn_month;
         $hn_year = (int) $pn_year;
         $hn_hour = (int) $pn_hour;
         $hn_minute = (int) $pn_minute;
         $hn_second = $pn_second;
         $hn_days = Date_Calc::dateToDays($pn_day, $pn_month, $pn_year);
         $hn_secondsofmonth = 86400 * ($hn_days - Date_Calc::firstDayOfMonth($pn_month, $pn_year)) + Date_Calc::secondsPastMidnight($pn_hour, $pn_minute, $pn_second);
         if ($hn_seconds > 0) {
             // Advance to end of month:
             //
             if ($hn_secondsofmonth != 0 && $hn_secondsofmonth + $hn_seconds >= ($hn_secondsinmonth = Date_Calc::getSecondsInMonth($hn_month, $hn_year))) {
                 $hn_seconds -= $hn_secondsinmonth - $hn_secondsofmonth;
                 $hn_secondsofmonth = 0;
                 list($hn_year, $hn_month) = Date_Calc::nextMonth($hn_month, $hn_year);
                 $hn_day = Date_Calc::getFirstDayOfMonth($hn_month, $hn_year);
                 $hn_hour = $hn_minute = $hn_second = 0;
             }
             // Advance to end of year:
             //
             if ($hn_secondsofmonth == 0 && $hn_month != Date_Calc::getFirstMonthOfYear($hn_year)) {
                 while ($hn_year == $pn_year && $hn_seconds >= ($hn_secondsinmonth = Date_Calc::getSecondsInMonth($hn_month, $hn_year))) {
                     $hn_seconds -= $hn_secondsinmonth;
                     list($hn_year, $hn_month) = Date_Calc::nextMonth($hn_month, $hn_year);
                     $hn_day = Date_Calc::getFirstDayOfMonth($hn_month, $hn_year);
                 }
             }
             if ($hn_secondsofmonth == 0) {
                 // Add years:
                 //
                 if ($hn_month == Date_Calc::getFirstMonthOfYear($hn_year)) {
                     while ($hn_seconds >= ($hn_secondsinyear = Date_Calc::getSecondsInYear($hn_year))) {
                         $hn_seconds -= $hn_secondsinyear;
                         $hn_month = Date_Calc::getFirstMonthOfYear(++$hn_year);
                         $hn_day = Date_Calc::getFirstDayOfMonth($hn_month, $hn_year);
                     }
                 }
                 // Add months:
                 //
                 while ($hn_seconds >= ($hn_secondsinmonth = Date_Calc::getSecondsInMonth($hn_month, $hn_year))) {
                     $hn_seconds -= $hn_secondsinmonth;
                     list($hn_year, $hn_month) = Date_Calc::nextMonth($hn_month, $hn_year);
                     $hn_day = Date_Calc::getFirstDayOfMonth($hn_month, $hn_year);
                 }
             }
         } else {
             //
             // (if $hn_seconds < 0)
             // Go back to start of month:
             //
             if ($hn_secondsofmonth != 0 && -$hn_seconds >= $hn_secondsofmonth) {
                 $hn_seconds += $hn_secondsofmonth;
                 $hn_secondsofmonth = 0;
                 $hn_day = Date_Calc::getFirstDayOfMonth($hn_month, $hn_year);
                 $hn_hour = $hn_minute = $hn_second = 0;
             }
             // Go back to start of year:
             //
             if ($hn_secondsofmonth == 0) {
                 while ($hn_month != Date_Calc::getFirstMonthOfYear($hn_year)) {
                     list($hn_year, $hn_prevmonth) = Date_Calc::prevMonth($hn_month, $hn_year);
                     if (-$hn_seconds >= ($hn_secondsinmonth = Date_Calc::getSecondsInMonth($hn_prevmonth, $hn_year))) {
                         $hn_seconds += $hn_secondsinmonth;
                         $hn_month = $hn_prevmonth;
                         $hn_day = Date_Calc::getFirstDayOfMonth($hn_month, $hn_year);
                     } else {
                         break;
                     }
                 }
             }
             if ($hn_secondsofmonth == 0) {
                 // Subtract years:
                 //
                 if ($hn_month == Date_Calc::getFirstMonthOfYear($hn_year)) {
                     while (-$hn_seconds >= ($hn_secondsinyear = Date_Calc::getSecondsInYear($hn_year - 1))) {
                         $hn_seconds += $hn_secondsinyear;
                         $hn_month = Date_Calc::getFirstMonthOfYear(--$hn_year);
                         $hn_day = Date_Calc::getFirstDayOfMonth($hn_month, $hn_year);
                     }
                 }
                 // Subtract months:
                 //
                 list($hn_pmyear, $hn_prevmonth) = Date_Calc::prevMonth($hn_month, $hn_year);
                 while (-$hn_seconds >= ($hn_secondsinmonth = Date_Calc::getSecondsInMonth($hn_prevmonth, $hn_pmyear))) {
                     $hn_seconds += $hn_secondsinmonth;
                     $hn_year = $hn_pmyear;
                     $hn_month = $hn_prevmonth;
                     $hn_day = Date_Calc::getFirstDayOfMonth($hn_month, $hn_year);
                     list($hn_pmyear, $hn_prevmonth) = Date_Calc::prevMonth($hn_month, $hn_year);
                 }
             }
         }
         if ($hn_seconds < 0 && $hn_secondsofmonth == 0) {
             list($hn_year, $hn_month) = Date_Calc::prevMonth($hn_month, $hn_year);
             $hn_day = Date_Calc::getFirstDayOfMonth($hn_month, $hn_year);
             $hn_seconds += Date_Calc::getSecondsInMonth($hn_month, $hn_year);
         }
         $hn_seconds += Date_Calc::secondsPastMidnight($hn_hour, $hn_minute, $hn_second);
         if ($hn_seconds < 0) {
             $hn_daysadd = intval($hn_seconds / 86400) - 1;
         } else {
             if ($hn_seconds < 86400) {
                 $hn_daysadd = 0;
             } else {
                 $hn_daysadd = intval($hn_seconds / 86400) - 1;
             }
         }
         if ($hn_daysadd != 0) {
             list($hn_year, $hn_month, $hn_day) = explode(" ", Date_Calc::addDays($hn_daysadd, $hn_day, $hn_month, $hn_year, "%Y %m %d"));
             $hn_seconds -= $hn_daysadd * 86400;
         }
         $hn_secondsinday = Date_Calc::getSecondsInDay($hn_day, $hn_month, $hn_year);
         if ($hn_seconds >= $hn_secondsinday) {
             list($hn_year, $hn_month, $hn_day) = explode(" ", Date_Calc::addDays(1, $hn_day, $hn_month, $hn_year, "%Y %m %d"));
             $hn_seconds -= $hn_secondsinday;
         }
         list($hn_hour, $hn_minute, $hn_second) = Date_Calc::secondsPastMidnightToTime($hn_seconds);
         return array((int) $hn_year, (int) $hn_month, (int) $hn_day, $hn_hour, $hn_minute, $hn_second);
     } else {
         // Assume every day has 86400 seconds exactly (ignore leap seconds):
         //
         $hn_minutes = intval($pn_seconds / 60);
         if (is_float($pn_seconds)) {
             $hn_second = $pn_second + fmod($pn_seconds, 60);
         } else {
             $hn_second = $pn_second + $pn_seconds % 60;
         }
         if ($hn_second >= 60) {
             ++$hn_minutes;
             $hn_second -= 60;
         } else {
             if ($hn_second < 0) {
                 --$hn_minutes;
                 $hn_second += 60;
             }
         }
         if ($hn_minutes == 0) {
             $hn_year = $pn_year;
             $hn_month = $pn_month;
             $hn_day = $pn_day;
             $hn_hour = $pn_hour;
             $hn_minute = $pn_minute;
         } else {
             list($hn_year, $hn_month, $hn_day, $hn_hour, $hn_minute) = Date_Calc::addMinutes($hn_minutes, $pn_day, $pn_month, $pn_year, $pn_hour, $pn_minute);
         }
         return array($hn_year, $hn_month, $hn_day, $hn_hour, $hn_minute, $hn_second);
     }
 }