⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 span.php

📁 太烦了
💻 PHP
📖 第 1 页 / 共 3 页
字号:
                            break;
                        case 's':
                            $str .= '%d';
                            array_push($vars, 'second');
                            break;
                        case 'S':
                            $str .= '%2d';
                            array_push($vars, 'second');
                            break;
                        case 't':
                            $str .= "\t";
                            break;
                        case 'T':
                            $str .= '%2d:%2d:%2d';
                            array_push($vars, 'hour', 'minute', 'second');
                            break;
                        case '%':
                            $str .= "%";
                            break;
                        default:
                            $str .= $char . $nextchar;
                    }
                } else {
                    $str .= $char;
                }
            }
            $vals = sscanf($time, $str);
            foreach ($vals as $i => $val) {
                if (is_null($val)) {
                    return false;
                }
                $$vars[$i] = $val;
            }
            if (strcasecmp($pm, 'pm') == 0) {
                $hour += 12;
            } elseif (strcasecmp($pm, 'am') != 0) {
                return false;
            }
            $this->setFromArray(array($day, $hour, $minute, $second));
        // If format is a integer, it uses a predefined format
        // detection method.
        } elseif (is_integer($format)) {
            switch ($format) {
                case DATE_SPAN_INPUT_FORMAT_NNSV:
                    $time = preg_split('/\D+/', $time);
                    switch (count($time)) {
                        case 0:
                            return $this->setFromArray(
                                array(0, 0, 0, 0));
                        case 1:
                            return $this->setFromArray(
                                array(0, $time[0], 0, 0));
                        case 2:
                            return $this->setFromArray(
                                array(0, $time[0], $time[1], 0));
                        case 3:
                            return $this->setFromArray(
                                array(0, $time[0], $time[1], $time[2]));
                        default:
                            return $this->setFromArray($time);
                    }
                    break;
            }
        }
        return false;
    }

    /**
     * Set the time span from a total number of seconds.
     *
     * @param  int  $seconds Total number of seconds.
     *
     * @return bool True on success.
     *
     * @access public
     */
    function setFromSeconds($seconds)
    {
        if ($seconds < 0) {
            return false;
        }
        $sec  = intval($seconds);
        $min  = floor($sec / 60);
        $hour = floor($min / 60);
        $day  = intval(floor($hour / 24));
        $this->second = $sec % 60;
        $this->minute = $min % 60;
        $this->hour   = $hour % 24;
        $this->day    = $day;
        return true;
    }

    /**
     * Set the time span from a total number of minutes.
     *
     * @param  float $minutes Total number of minutes.
     *
     * @return bool  True on success.
     *
     * @access public
     */
    function setFromMinutes($minutes)
    {
        return $this->setFromSeconds(round($minutes * 60));
    }

    /**
     * Set the time span from a total number of hours.
     *
     * @param  float $hours Total number of hours.
     *
     * @return bool  True on success.
     *
     * @access public
     */
    function setFromHours($hours)
    {
        return $this->setFromSeconds(round($hours * 3600));
    }

    /**
     * Set the time span from a total number of days.
     *
     * @param  float $days Total number of days.
     *
     * @return bool  True on success.
     *
     * @access public
     */
    function setFromDays($days)
    {
        return $this->setFromSeconds(round($days * 86400));
    }

    /**
     * Set the span from the elapsed time between two dates.
     *
     * Set the span from the elapsed time between two dates. The time span
     * is allways positive, so the date's order is not important.
     * 
     * @param  object Date $date1 First Date.
     * @param  object Date $date2 Second Date.
     *
     * @return bool  True on success.
     *
     * @access public
     */
    function setFromDateDiff($date1, $date2)
    {
        if (!is_a($date1, 'date') or !is_a($date2, 'date')) {
            return false;
        }
        $date1->toUTC();
        $date2->toUTC();
        if ($date1->after($date2)) {
            list($date1, $date2) = array($date2, $date1);
        }
        $days = Date_Calc::dateDiff(
            $date1->getDay(), $date1->getMonth(), $date1->getYear(),
            $date2->getDay(), $date2->getMonth(), $date2->getYear()
        );
        $hours = $date2->getHour() - $date1->getHour();
        $mins  = $date2->getMinute() - $date1->getMinute();
        $secs  = $date2->getSecond() - $date1->getSecond();
        $this->setFromSeconds(
            $days * 86400 + $hours * 3600 + $mins * 60 + $secs
        );
        return true;
    }

    /**
     * Set the time span from another time object.
     *
     * @param  object Date_Span $time Source time span object.
     *
     * @return bool   True on success.
     *
     * @access public
     */
    function copy($time)
    {
        if (is_a($time, 'date_span')) {
            $this->second = $time->second;
            $this->minute = $time->minute;
            $this->hour   = $time->hour;
            $this->day    = $time->day;
            return true;
        } else {
            return false;
        }
    }

    /**
     * Time span pretty printing (similar to Date::format()).
     *
     * Formats the time span in the given format, similar to
     * strftime() and Date::format().<br>
     * <br>
     * Formatting options:<br>
     *  <code>%C</code> Days with time, same as "%D, %H:%M:%S".<br>
     *  <code>%d</code> Total days as a float number
     *                  (2 days, 12 hours = 2.5 days).<br>
     *  <code>%D</code> Days as a decimal number.<br>
     *  <code>%e</code> Total hours as a float number
     *                  (1 day, 2 hours, 30 minutes = 26.5 hours).<br>
     *  <code>%E</code> Total hours as a decimal number
     *                  (1 day, 2 hours, 40 minutes = 26 hours).<br>
     *  <code>%f</code> Total minutes as a float number
     *                  (2 minutes, 30 seconds = 2.5 minutes).<br>
     *  <code>%F</code> Total minutes as a decimal number
     *                  (1 hour, 2 minutes, 40 seconds = 62 minutes).<br>
     *  <code>%g</code> Total seconds as a decimal number
     *                  (2 minutes, 30 seconds = 90 seconds).<br>
     *  <code>%h</code> Hours as decimal number (0 to 23).<br>
     *  <code>%H</code> Hours as decimal number (00 to 23).<br>
     *  <code>%i</code> Hours as decimal number on 12-hour clock
     *                  (1 to 12).<br>
     *  <code>%I</code> Hours as decimal number on 12-hour clock
     *                  (01 to 12).<br>
     *  <code>%m</code> Minutes as a decimal number (0 to 59).<br>
     *  <code>%M</code> Minutes as a decimal number (00 to 59).<br>
     *  <code>%n</code> Newline character (\n).<br>
     *  <code>%p</code> Either 'am' or 'pm' depending on the time.<br>
     *  <code>%P</code> Either 'AM' or 'PM' depending on the time.<br>
     *  <code>%r</code> Time in am/pm notation, same as "%I:%M:%S %p".<br>
     *  <code>%R</code> Time in 24-hour notation, same as "%H:%M".<br>
     *  <code>%s</code> Seconds as a decimal number (0 to 59).<br>
     *  <code>%S</code> Seconds as a decimal number (00 to 59).<br>
     *  <code>%t</code> Tab character (\t).<br>
     *  <code>%T</code> Current time equivalent, same as "%H:%M:%S".<br>
     *  <code>%%</code> Literal '%'.<br>
     *
     * @param  string $format The format string for returned time span.
     *
     * @return string The time span in specified format.
     *
     * @access public
     */
    function format($format = null)
    {
        if (is_null($format)) {
            $format = $GLOBALS['_DATE_SPAN_FORMAT'];
        }
        $output = '';
        for ($i = 0; $i < strlen($format); $i++) {
            $char = $format{$i};
            if ($char == '%') {
                $nextchar = $format{++$i};
                switch ($nextchar) {
                    case 'C':
                        $output .= sprintf(
                            '%d, %02d:%02d:%02d',
                            $this->day,
                            $this->hour,
                            $this->minute,
                            $this->second
                        );
                        break;
                    case 'd':
                        $output .= $this->toDays();
                        break;
                    case 'D':
                        $output .= $this->day;
                        break;
                    case 'e':
                        $output .= $this->toHours();
                        break;
                    case 'E':
                        $output .= floor($this->toHours());
                        break;
                    case 'f':
                        $output .= $this->toMinutes();
                        break;
                    case 'F':
                        $output .= floor($this->toMinutes());
                        break;
                    case 'g':
                        $output .= $this->toSeconds();
                        break;
                    case 'h':
                        $output .= $this->hour;
                        break;
                    case 'H':
                        $output .= sprintf('%02d', $this->hour);
                        break;
                    case 'i':
                        $hour =
                            ($this->hour + 1) > 12 ?
                            $this->hour - 12 :
                            $this->hour;
                        $output .= ($hour == 0) ? 12 : $hour;
                        break;
                    case 'I':
                        $hour =
                            ($this->hour + 1) > 12 ?
                            $this->hour - 12 :
                            $this->hour;
                        $output .= sprintf('%02d', $hour==0 ? 12 : $hour);
                        break;
                    case 'm':
                        $output .= $this->minute;
                        break;
                    case 'M':
                        $output .= sprintf('%02d',$this->minute);
                        break;
                    case 'n':
                        $output .= "\n";
                        break;
                    case 'p':
                        $output .= $this->hour >= 12 ? 'pm' : 'am';
                        break;
                    case 'P':
                        $output .= $this->hour >= 12 ? 'PM' : 'AM';
                        break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -