📄 span.php
字号:
<?php
// vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Leandro Lucarella <llucax@php.net> |
// +----------------------------------------------------------------------+
//
// $Id: Span.php,v 1.4 2003/04/30 03:56:26 llucax Exp $
//
require_once PEAR_DIR . 'Date.php';
require_once PEAR_DIR . 'Date/Calc.php';
/**
* Non Numeric Separated Values (NNSV) Input Format.
*
* Input format guessed from something like this:
* days<sep>hours<sep>minutes<sep>seconds
* Where <sep> is any quantity of non numeric chars. If no values are
* given, time span is set to zero, if one value is given, it's used for
* hours, if two values are given it's used for hours and minutes and if
* three values are given, it's used for hours, minutes and seconds.<br>
* Examples:<br>
* '' -> 0, 0, 0, 0 (days, hours, minutes, seconds)<br>
* '12' -> 0, 12, 0, 0
* '12.30' -> 0, 12, 30, 0<br>
* '12:30:18' -> 0, 12, 30, 18<br>
* '3-12-30-18' -> 3, 12, 30, 18<br>
* '3 days, 12-30-18' -> 3, 12, 30, 18<br>
* '12:30 with 18 secs' -> 0, 12, 30, 18<br>
*
* @const int
*/
define('DATE_SPAN_INPUT_FORMAT_NNSV', 1);
/**
* Default time format when converting to a string.
*
* @global string
*/
$_DATE_SPAN_FORMAT = '%C';
/**
* Default time format when converting from a string.
*
* @global mixed
*/
$_DATE_SPAN_INPUT_FORMAT = DATE_SPAN_INPUT_FORMAT_NNSV;
/**
* Generic time span handling class for PEAR.
*
* @package Date
* @author Leandro Lucarella <llucax@php.net>
* @version $Revision: 1.4 $
* @since 1.4
* @todo Get and set default local input and output formats?
* @access public
*/
class Date_Span {
/**
* @var int
*/
var $day;
/**
* @var int
*/
var $hour;
/**
* @var int
*/
var $minute;
/**
* @var int
*/
var $second;
/**
* Constructor.
*
* Creates the time span object calling the set() method.
*
* @param mixed $time Time span expression.
* @param mixed $format Format string to set it from a string or the
* second date set it from a date diff.
*
* @see set()
* @access public
*/
function Date_Span($time = 0, $format = null)
{
$this->set($time, $format);
}
/**
* Set the time span to a new value in a 'smart' way.
*
* Sets the time span depending on the argument types, calling
* to the appropriate setFromXxx() method.
*
* @param mixed $time Time span expression.
* @param mixed $format Format string to set it from a string or the
* second date set it from a date diff.
*
* @return bool true on success.
*
* @see setFromObject()
* @see setFromArray()
* @see setFromString()
* @see setFromSeconds()
* @see setFromDateDiff()
* @access public
*/
function set($time = 0, $format = null)
{
if (is_a($time, 'date_span')) {
return $this->copy($time);
} elseif (is_a($time, 'date') and is_a($format, 'date')) {
return $this->setFromDateDiff($time, $format);
} elseif (is_array($time)) {
return $this->setFromArray($time);
} elseif (is_string($time)) {
return $this->setFromString($time, $format);
} elseif (is_int($time)) {
return $this->setFromSeconds($time);
} else {
return $this->setFromSeconds(0);
}
}
/**
* Set the time span from an array.
*
* Set the time span from an array. Any value can be a float (but it
* has no sense in seconds), for example array(23.5, 20, 0) is
* interpreted as 23 hours, .5*60 + 20 = 50 minutes and 0 seconds.
*
* @param array $time Items are counted from right to left. First
* item is for seconds, second for minutes, third
* for hours and fourth for days. If there are
* less items than 4, zero (0) is assumed for the
* absent values.
*
* @return bool True on success.
*
* @access public
*/
function setFromArray($time)
{
if (!is_array($time)) {
return false;
}
$tmp1 = new Date_Span;
if (!$tmp1->setFromSeconds(@array_pop($time))) {
return false;
}
$tmp2 = new Date_Span;
if (!$tmp2->setFromMinutes(@array_pop($time))) {
return false;
}
$tmp1->add($tmp2);
if (!$tmp2->setFromHours(@array_pop($time))) {
return false;
}
$tmp1->add($tmp2);
if (!$tmp2->setFromDays(@array_pop($time))) {
return false;
}
$tmp1->add($tmp2);
return $this->copy($tmp1);
}
/**
* Set the time span from a string based on an input format.
*
* Set the time span from a string based on an input format. This is
* some like a mix of format() method and sscanf() PHP function. The
* error checking and validation of this function is very primitive,
* so you should be carefull when using it with unknown $time strings.
* With this method you are assigning day, hour, minute and second
* values, and the last values are used. This means that if you use
* something like setFromString('10, 20', '%H, %h') your time span
* would be 20 hours long. Allways remember that this method set
* <b>all</b> the values, so if you had a $time span 30 minutes long
* and you make $time->setFromString('20 hours', '%H hours'), $time
* span would be 20 hours long (and not 20 hours and 30 minutes).
* Input format 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>%f</code> Total minutes as a float number
* (2 minutes, 30 seconds = 2.5 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.<br>
* <code>%H</code> Hours as decimal number limited to 2 digits.<br>
* <code>%m</code> Minutes as a decimal number.<br>
* <code>%M</code> Minutes as a decimal number limited to 2 digits.<br>
* <code>%n</code> Newline character (\n).<br>
* <code>%p</code> Either 'am' or 'pm' depending on the time. If 'pm'
* is detected it adds 12 hours to the resulting time
* span (without any checks). This is case
* insensitive.<br>
* <code>%r</code> Time in am/pm notation, same as "%H:%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.<br>
* <code>%S</code> Seconds as a decimal number limited to 2 digits.<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 $time String from where to get the time span
* information.
* @param string $format Format string.
*
* @return bool True on success.
*
* @access public
*/
function setFromString($time, $format = null)
{
if (is_null($format)) {
$format = $GLOBALS['_DATE_SPAN_INPUT_FORMAT'];
}
// If format is a string, it parses the string format.
if (is_string($format)) {
$str = '';
$vars = array();
$pm = 'am';
$day = $hour = $minute = $second = 0;
for ($i = 0; $i < strlen($format); $i++) {
$char = $format{$i};
if ($char == '%') {
$nextchar = $format{++$i};
switch ($nextchar) {
case 'c':
$str .= '%d, %d:%d:%d';
array_push(
$vars, 'day', 'hour', 'minute', 'second');
break;
case 'C':
$str .= '%d, %2d:%2d:%2d';
array_push(
$vars, 'day', 'hour', 'minute', 'second');
break;
case 'd':
$str .= '%f';
array_push($vars, 'day');
break;
case 'D':
$str .= '%d';
array_push($vars, 'day');
break;
case 'e':
$str .= '%f';
array_push($vars, 'hour');
break;
case 'f':
$str .= '%f';
array_push($vars, 'minute');
break;
case 'g':
$str .= '%f';
array_push($vars, 'second');
break;
case 'h':
$str .= '%d';
array_push($vars, 'hour');
break;
case 'H':
$str .= '%2d';
array_push($vars, 'hour');
break;
case 'm':
$str .= '%d';
array_push($vars, 'minute');
break;
case 'M':
$str .= '%2d';
array_push($vars, 'minute');
break;
case 'n':
$str .= "\n";
break;
case 'p':
$str .= '%2s';
array_push($vars, 'pm');
break;
case 'r':
$str .= '%2d:%2d:%2d %2s';
array_push(
$vars, 'hour', 'minute', 'second', 'pm');
break;
case 'R':
$str .= '%2d:%2d';
array_push($vars, 'hour', 'minute');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -