Book/datetime-Phpdoc专题
日期和时间
目录
- 简介
- 安装/配置
- 预定义常量
- 范例
- DateTime — DateTime 类
- DateTime::add — 给一个 DateTime 对象增加一定量的天,月,年,小时,分钟 以及秒。
- DateTime::__construct — 返回一个新的 DateTime 对象
- DateTime::createFromFormat — 根据给定的格式解析日期时间字符串
- DateTime::createFromImmutable — Returns new DateTime object encapsulating the given DateTimeImmutable object
- DateTime::createFromInterface — Returns new DateTime object encapsulating the given DateTimeInterface object
- DateTime::getLastErrors — 获取警告和错误信息
- DateTime::modify — 修改日期时间对象的值
- DateTime::__set_state — __set_state 魔术方法处理函数
- DateTime::setDate — 设置 DateTime 对象的日期
- DateTime::setISODate — 设置 ISO 日期
- DateTime::setTime — 设置 DateTime 对象的时间
- DateTime::setTimestamp — 以 Unix 时间戳的方式设置 DateTime 对象
- DateTime::setTimezone — 设置 DateTime 对象的时区
- DateTime::sub — 对一个 DateTime 对象减去一定量的 日、月、年、小时、分钟和秒。
- DateTimeImmutable — The
DateTimeImmutable class
- DateTimeImmutable::add — Adds an amount of days, months, years, hours, minutes and seconds
- DateTimeImmutable::__construct — Returns new DateTimeImmutable object
- DateTimeImmutable::createFromFormat — Parses a time string according to a specified format
- DateTimeImmutable::createFromInterface — Returns new DateTimeImmutable object encapsulating the given DateTimeInterface object
- DateTimeImmutable::createFromMutable — Returns new DateTimeImmutable object encapsulating the given DateTime object
- DateTimeImmutable::getLastErrors — Returns the warnings and errors
- DateTimeImmutable::modify — Creates a new object with modified timestamp
- DateTimeImmutable::__set_state — The __set_state handler
- DateTimeImmutable::setDate — Sets the date
- DateTimeImmutable::setISODate — Sets the ISO date
- DateTimeImmutable::setTime — Sets the time
- DateTimeImmutable::setTimestamp — Sets the date and time based on a Unix timestamp
- DateTimeImmutable::setTimezone — Sets the time zone
- DateTimeImmutable::sub — Subtracts an amount of days, months, years, hours, minutes and seconds
- DateTimeInterface — The
DateTimeInterface interface
- DateTime::diff — Returns the difference between two DateTime objects
- DateTime::format — Returns date formatted according to given format
- DateTime::getOffset — Returns the timezone offset
- DateTime::getTimestamp — Gets the Unix timestamp
- DateTime::getTimezone — Return time zone relative to given DateTime
- DateTime::__wakeup — The __wakeup handler
- DateTimeZone — The DateTimeZone class
- DateTimeZone::__construct — 创建新的DateTimeZone对象
- DateTimeZone::getLocation — 返回与时区相关的定位信息。
- DateTimeZone::getName — 返回时区名称。
- DateTimeZone::getOffset — 返回相对于 GMT 的时差。
- DateTimeZone::getTransitions — Returns all transitions for the timezone
- DateTimeZone::listAbbreviations — 返回一个包含 dst (夏令时),时差和时区信息的关联数组。
- DateTimeZone::listIdentifiers — 返回一个包含了所有时区标示符的索引数组。
- DateInterval — DateInterval 类
- DateInterval::__construct — Creates a new DateInterval object
- DateInterval::createFromDateString — Sets up a DateInterval from the relative parts of the string
- DateInterval::format — Formats the interval
- DatePeriod — DatePeriod 类
- DatePeriod::__construct — Creates a new DatePeriod object
- DatePeriod::getDateInterval — Gets the interval
- DatePeriod::getEndDate — Gets the end date
- DatePeriod::getRecurrences — Gets the number of recurrences
- DatePeriod::getStartDate — Gets the start date
- Date/Time 函数
- checkdate — 验证一个格里高里日期
- date_add — 别名 DateTime::add
- date_create_from_format — 别名 DateTime::createFromFormat
- date_create_immutable_from_format — 别名 DateTimeImmutable::createFromFormat
- date_create_immutable — 别名 DateTimeImmutable::__construct
- date_create — 别名 DateTime::__construct
- date_date_set — 别名 DateTime::setDate
- date_default_timezone_get — 取得一个脚本中所有日期时间函数所使用的默认时区
- date_default_timezone_set — 设定用于一个脚本中所有日期时间函数的默认时区
- date_diff — 别名 DateTime::diff
- date_format — 别名 DateTime::format
- date_get_last_errors — 别名 DateTime::getLastErrors
- date_interval_create_from_date_string — 别名 DateInterval::createFromDateString
- date_interval_format — 别名 DateInterval::format
- date_isodate_set — 别名 DateTime::setISODate
- date_modify — 别名 DateTime::modify
- date_offset_get — 别名 DateTime::getOffset
- date_parse_from_format — Get info about given date formatted according to the specified format
- date_parse — Returns associative array with detailed info about given date/time
- date_sub — 别名 DateTime::sub
- date_sun_info — Returns an array with information about sunset/sunrise and twilight begin/end
- date_sunrise — 返回给定的日期与地点的日出时间
- date_sunset — 返回给定的日期与地点的日落时间
- date_time_set — 别名 DateTime::setTime
- date_timestamp_get — 别名 DateTime::getTimestamp
- date_timestamp_set — 别名 DateTime::setTimestamp
- date_timezone_get — 别名 DateTime::getTimezone
- date_timezone_set — 别名 DateTime::setTimezone
- date — 格式化一个本地时间/日期
- getdate — 取得日期/时间信息
- gettimeofday — 取得当前时间
- gmdate — 格式化一个 GMT/UTC 日期/时间
- gmmktime — 取得 GMT 日期的 UNIX 时间戳
- gmstrftime — 根据区域设置格式化 GMT/UTC 时间/日期
- idate — 将本地时间日期格式化为整数
- localtime — 取得本地时间
- microtime — 返回当前 Unix 时间戳和微秒数
- mktime — 取得一个日期的 Unix 时间戳
- strftime — 根据区域设置格式化本地时间/日期
- strptime — 解析由 strftime 生成的日期/时间
- strtotime — 将任何字符串的日期时间描述解析为 Unix 时间戳
- time — 返回当前的 Unix 时间戳
- timezone_abbreviations_list — 别名 DateTimeZone::listAbbreviations
- timezone_identifiers_list — 别名 DateTimeZone::listIdentifiers
- timezone_location_get — 别名 DateTimeZone::getLocation
- timezone_name_from_abbr — Returns the timezone name from abbreviation
- timezone_name_get — 别名 DateTimeZone::getName
- timezone_offset_get — 别名 DateTimeZone::getOffset
- timezone_open — 别名 DateTimeZone::__construct
- timezone_transitions_get — 别名 DateTimeZone::getTransitions
- timezone_version_get — Gets the version of the timezonedb
- Supported Date and Time Formats
- 所支持的时区列表
简介
日期和时间。
类摘要
DateTime
class DateTime <span class="oointerface">implements <span class="interfacename">DateTimeInterface {
/* 继承的常量 */
const string
DateTimeInterface::ATOM
=
"Y-m-d\TH:i:sP" ;
const string
DateTimeInterface::COOKIE
= "l, d-M-Y H:i:s
T" ;
const string
DateTimeInterface::ISO8601
=
"Y-m-d\TH:i:sO" ;
const string
DateTimeInterface::RFC822
= "D, d M y H:i:s
O" ;
const string
DateTimeInterface::RFC850
= "l, d-M-y H:i:s
T" ;
const string
DateTimeInterface::RFC1036
= "D, d M y
H:i:s O" ;
const string
DateTimeInterface::RFC1123
= "D, d M Y
H:i:s O" ;
const string
DateTimeInterface::RFC7231
= "D, d M Y
H:i:s \G\M\T" ;
const string
DateTimeInterface::RFC2822
= "D, d M Y
H:i:s O" ;
const string
DateTimeInterface::RFC3339
=
"Y-m-d\TH:i:sP" ;
const string
DateTimeInterface::RFC3339_EXTENDED
=
"Y-m-d\TH:i:s.vP" ;
const string
DateTimeInterface::RSS
= "D, d M Y H:i:s
O" ;
const string
DateTimeInterface::W3C
=
"Y-m-d\TH:i:sP" ;
/* 方法 */
public <span
class="methodname">__construct ([ <span
class="methodparam">string $time
<span
class="initializer"> = "now" [, <span
class="methodparam">DateTimeZone
$timezone
= null
]] )
public DateTime
add ( <span
class="type">DateInterval $interval
)
public <span
class="modifier">static DateTime <span
class="methodname">createFromFormat ( <span
class="methodparam">string $format
,
string
$time
[, <span
class="type">DateTimeZone $timezone
] )
public <span
class="modifier">static DateTime <span
class="methodname">createFromImmutable ( <span
class="methodparam">DateTimeImmutable
$object
)
public <span
class="modifier">static DateTime <span
class="methodname">createFromInterface ( <span
class="methodparam">DateTimeInterface
$object
)
public <span class="modifier">static array <span class="methodname">getLastErrors ( <span class="methodparam">void )
public DateTime
modify ( <span
class="type">string $modifier
)
public <span
class="modifier">static DateTime <span
class="methodname">__set_state ( <span
class="methodparam">array $array
)
public DateTime
setDate ( <span
class="methodparam">int $year
, <span
class="methodparam">int $month
,
int $day
)
public DateTime
setISODate ( <span
class="methodparam">int $year
, <span
class="methodparam">int $week
[,
int
$dayOfWeek
= 1 ] )
public DateTime
setTime ( <span
class="methodparam">int $hour
, <span
class="methodparam">int $minute
[,
int $second
<span
class="initializer"> = 0 [, <span
class="methodparam">int $microsecond
<span
class="initializer"> = 0 ]] )
public DateTime
setTimestamp ( <span
class="methodparam">int
$unixtimestamp
)
public DateTime
setTimezone ( <span
class="methodparam">DateTimeZone
$timezone
)
public DateTime
sub ( <span
class="type">DateInterval $interval
)
public <span
class="type">DateIntervalfalse
diff ( <span
class="type">DateTimeInterface $targetObject
[, <span
class="methodparam">bool $absolute
<span
class="initializer"> = false
] )
public string
format ( <span
class="type">string $format
)
publicint <span class="methodname">getOffset ( <span class="methodparam">void )
public int <span class="methodname">getTimestamp ( <span class="methodparam">void )
public <span class="type">DateTimeZonefalse getTimezone ( <span class="methodparam">void )
public void __wakeup ( <span class="methodparam">void )
}
更新日志
版本 | 说明 |
---|---|
7.2.0 | DateTime 的类常量现在定义在 DateTimeInterface 上。 |
7.0.0 | 新增常量:DATE_RFC3339_EXTENDED 和 DateTime::RFC3339_EXTENDED。 |
5.5.0 | 实现 DateTimeInterface 接口。 |
5.4.24 | COOKIE 格式从 2 位数字表示年份(RFC 850) 修改为 4 位数字表示年份(RFC 1036)。 |
5.2.2 | DateTime 对象进行比较操作(comparison operators)的时候 可以正常工作了。 在之前的版本中,当使用 == 进行相等比较的时候, 所有的 DateTime 对象都会被视为是相等的。 |
DateTime::add
date_add
给一个 DateTime 对象增加一定量的天,月,年,小时,分钟 以及秒。
说明
面向对象风格
public DateTime
DateTime::add ( <span
class="methodparam">DateInterval
$interval
)
过程化风格
DateTime <span
class="methodname">date_add ( <span
class="type">DateTime $object
, <span
class="methodparam">DateInterval
$interval
)
将给定的 DateInterval 对象 加到 <span class="classname">DateTime 对象上。
参数
object
仅过程化风格:由 date_create 返回的 <span
class="classname">DateTime 类型的对象。此函数会修改这个对象。
interval
DateInterval 对象。
返回值
返回被修改的 DateTime 对象, 或者在失败时返回 false
.
范例
示例 #1 DateTime::add 例程
面向对象风格
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>
过程化风格
<?php
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');
?>
以上例程会输出:
2000-01-11
示例 #2 Further DateTime::add 例程
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
以上例程会输出:
2000-01-01 10:00:30
2007-06-05 04:03:02
示例 #3 当在 DateTime 上加月的时候要注意
<?php
$date = new DateTime('2000-12-31');
$interval = new DateInterval('P1M');
$date->add($interval);
echo $date->format('Y-m-d') . "\n";
$date->add($interval);
echo $date->format('Y-m-d') . "\n";
?>
以上例程会输出:
2001-01-31
2001-03-03
注释
在 PHP 5.2 的版本中, 也可以使用 <span class="function">DateTime::modify 方法来替代本方法。
参见
- DateTime::sub
- DateTime::diff
- DateTime::modify
DateTime::__construct
date_create
返回一个新的 DateTime 对象
说明
面向对象风格
public <span
class="methodname">DateTime::__construct ([ <span
class="methodparam">string $time
<span
class="initializer"> = "now" [, <span
class="methodparam">DateTimeZone
$timezone
= null
]] )
过程化风格
DateTime <span
class="methodname">date_create ([ <span
class="methodparam">string $time
<span
class="initializer"> = "now" [, <span
class="methodparam">DateTimeZone
$timezone
= null
]] )
返回一个新的 DateTime 对象。
参数
time
日期/时间字符串。正确格式的说明详见
日期与时间格式。
如果这个参数为字符串 "now" 表示获取当前时间。 如果同时指定了
$timezone
参数,那么获取指定时区的当前时间。
timezone
DateTimeZone 对象, 表示要获取哪个时区的
$time
。
如果省略了 $timezone
参数, 那么会使用当前时区。
Note:
当
$time
参数是 UNIX 时间戳 (例如 @946684800), 或者已经包含时区信息 (例如 2010-01-28T15:00:00+02:00)的时候,$timezone
参数 和当前时区都将被忽略。
返回值
返回一个新的 DateTime 对象实例,或者在发生错误的时候返回
过程化风格在失败时返回 false
。。
错误/异常
如果发生错误,会抛出 Exception。
更新日志
版本 | 说明 |
---|---|
7.1 | 微秒部分不再是 '00000' 了,而是真实的微秒数据。 |
5.3.0 | 如果 time 参数不是一个有效的 日期/时间格式, 会抛出异常。 在之前的版本中是会发出一个错误。 |
范例
示例 #1 DateTime::__construct 例程
面向对象风格
<?php
try {
$date = new DateTime('2000-01-01');
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
echo $date->format('Y-m-d');
?>
过程化风格
<?php
$date = date_create('2000-01-01');
if (!$date) {
$e = date_get_last_errors();
foreach ($e['errors'] as $error) {
echo "$error\n";
}
exit(1);
}
echo date_format($date, 'Y-m-d');
?>
以上例程会输出:
2000-01-01
示例 #2 DateTime::__construct 的复杂用法
<?php
// 指定时间,但是使用电脑的时区
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:sP') . "\n";
// 指定时间和时区
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
// 使用当前时间以及电脑的时区
$date = new DateTime();
echo $date->format('Y-m-d H:i:sP') . "\n";
// 使用当前时间和指定的时区
$date = new DateTime(null, new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
// 使用 UNIX 时间戳作为时间,请注意这里的生成的 DateTime 对象对应的是 UTC 时区
$date = new DateTime('@946684800');
echo $date->format('Y-m-d H:i:sP') . "\n";
// 指定一个无效的时间,会自动对应到有效的时间
$date = new DateTime('2000-02-30');
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
以上例程的输出类似于:
2000-01-01 00:00:00-05:00
2000-01-01 00:00:00+12:00
2010-04-24 10:24:16-04:00
2010-04-25 02:24:16+12:00
2000-01-01 00:00:00+00:00
2000-03-01 00:00:00-05:00
参见
- DateTime::createFromFormat
- DateTimeZone::__construct
- 日期时间格式
- date.timezone ini 设置
- date_default_timezone_set
- DateTime::getLastErrors
- checkdate
DateTime::createFromFormat
date_create_from_format
根据给定的格式解析日期时间字符串
说明
面向对象风格
public <span
class="modifier">static DateTime <span
class="methodname">DateTime::createFromFormat ( <span
class="methodparam">string $format
,
string
$time
[, <span
class="type">DateTimeZone $timezone
] )
过程化风格
DateTime <span
class="methodname">date_create_from_format ( <span
class="methodparam">string $format
,
string
$time
[, <span
class="type">DateTimeZone $timezone
] )
将 time
参数给定的日期时间字符串, 根据 format
参数给定的格式
解析为一个新的 DateTime 对象。
参数
format
在解析日期时间字符串的时候使用的格式 string。
参加下列的格式清单。 大部分格式和 date
函数中的格式是一致的。
format 中的字符 |
解释 | 示例 |
---|---|---|
日 | --- | --- |
d 和 j | 一个月中的第几天,2 位数字表示,有前导 0 或者无前导 0 | 01 到 31 或者 1 到 31 |
D 和 l | 星期几的文字表示 | Mon 到 Sun 或者 Sunday 到 Saturday |
S | 2 个字母表示的一个月中的第几天(序数词), 在进行解析的时候会被忽略 | st,nd,rd 或者 th。 |
z | 一年中的第几天,从 0 开始 | 0 到 365 |
月 | --- | --- |
F 和 M | 文本表示的月份,例如 January 或者 Sept | January 到 December 或者 Jan 到 Dec |
m 和 n | 数值表示的月份,有前导 0 或者无前导 0 | 01 到 12 or 1 到 12 |
年 | --- | --- |
Y | 4 位数字表示的年 | 例如:1999 或 2003 |
y | 2 位数字表示的年, 可用的范围是 1970 至 2069(不含) | 例如: 99 或 03 (表示 1999 和 2003) |
时间 | --- | --- |
a 和 A | 上午、下午 | am 或 pm |
g and h | 12 小时制的小时,有前导 0 或者无前导 0 | 1 到 12 或者 01 到 12 |
G 和 H | 24 小时制的小时,有前导 0 或者无前导 0 | 0 到 23 或 00 到 23 |
i | 分钟,有前导 0 | 00 到 59 |
s | 秒,有前导 0 | 00 到 59 |
u | 微秒,最多到 6 位数字 | 示例:45,654321 |
时区 | --- | --- |
e,O, P 和 T | 时区名称,或者是以 UTC 时区为基准的小时偏移量, 或者是以 UTC 为基准的小时和分钟的偏移量, 小时和分钟之间用冒号(:)分隔。 | 示例:UTC,GMT, Atlantic/Azores 或 +0200 或 +02:00 或 EST,MDT |
完整的日期和时间 | --- | --- |
U | 从 Unix Epoch (January 1 1970 00:00:00 GMT) 开始计算的时间,以秒为单位 | 示例:1292177455 |
空白字符和分隔字符 | --- | --- |
(空格) | 一个空格字符或者一个 tab 字符 | 示例: |
# | 可以是一下分隔符号中的任意一个: ;, :,/,., ,,-,( 或 ) | 示例:/ |
;, :,/,., ,,-,( 或 ) | 特殊字符 | 示例:- |
? | 随机字节 | 示例:^ (需要注意的是, 对于 UTF-8 字符,可能会需要多个 ?。 这种情况下,请使用 *) |
* | 随机字节,直到遇到下一个有效的分隔符号或者数值 | 示例:使用 Y-*-d 格式用来解析 2009-aWord-08 字符串的时候, * 会匹配 aWord |
! | 将所有的字段(年、月、日、时、分、秒、微秒以及时区)重置到 Unix Epoch 时间。 | 如果不使用 !, 格式, 那么所有的字段会被设置为系统当前的日期和时间。 |
| | 将尚未被解析的字段,也即格式字符串中未明确指定的字段 (年、月、日、时、分、秒、微秒以及时区) 重置到 Unix Epoch 时间。 | Y-m-d| 会解析日期时间字符串中的年、月和日, 但是对于时、分、秒字段会设置为 0. |
+ | 在格式字符串中使用这个格式表示字符, 并且所提供的日期时间字符串中包含除了格式字符之外的其他数据的话,不会发出一个错误,而是发出一个警告。 | 使用 DateTime::getLastErrors 方法 来检测所给定的日期时间字符串中是否包含格式字符串指定的内容之外的数据。 |
如果在格式字符串中包含不可识别的字符, 那么会导致解析失败,并且在返回的结构中附加一个错误信息。 可以通过 <span class="methodname">DateTime::getLastErrors 来探查解析是否存在错误。
如果需要在格式字符串 format
参数中使用
上述表示格式的字符作为一个普通字符,请对其使用反斜线(\)进行转义。
如果格式字符串参数 format
中不包含 ! 字符, 那么没有在 format
参数中指明的字段, 在解析结果中将会被设置为系统当前时间对应的字段值。
如果格式字符串参数 format
包含了 ! 字符, 那么没有在 format
参数中指明的字段, 以及在 ! 左侧对应的字段, 在解析结果中将会被设置为
Unix epoch 时间对应的字段。
The Unix epoch 为 1970-01-01 00:00:00 UTC。
time
用来表示日期时间的字符串。
timezone
DateTimeZone 对象,
表示在解析日期时间字符串的时候需要使用的时区。
如果忽略 timezone
参数, 并且表示日期时间的字符串 time
中也不包含时区信息, 那么将会使用系统当前时区作为解析结果对象的时区。
Note:
如果
time
参数 是 UNIX 时间戳格式(例如:946684800), 或者其中已经包含了时区信息(例如:2010-01-28T15:00:00+02:00), 那么timezone
以及系统当前时区 都将会被忽略。
返回值
返回一个 DateTime 对象。 或者在失败时返回 false
。
更新日志
版本 | 说明 |
---|---|
5.3.9 | 新增 format 格式字符串中对于 + 格式字符的支持。 |
范例
示例 #1 DateTime::createFromFormat 例程
面向对象风格
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
过程化风格
<?php
$date = date_create_from_format('j-M-Y', '15-Feb-2009');
echo date_format($date, 'Y-m-d');
?>
以上例程会输出:
2009-02-15
示例 #2 DateTime::createFromFormat 的复杂用法
<?php
echo 'Current time: ' . date('Y-m-d H:i:s') . "\n";
$format = 'Y-m-d';
$date = DateTime::createFromFormat($format, '2009-02-15');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
$format = 'Y-m-!d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
$format = '!d';
$date = DateTime::createFromFormat($format, '15');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
?>
以上例程的输出类似于:
Current time: 2010-04-23 10:29:35
Format: Y-m-d; 2009-02-15 10:29:35
Format: Y-m-d H:i:s; 2009-02-15 15:16:17
Format: Y-m-!d H:i:s; 1970-01-15 15:16:17
Format: !d; 1970-01-15 00:00:00
示例 #3 格式化字符串中包含了需要进行转义的字符
<?php
echo DateTime::createFromFormat('H\h i\m s\s','23h 15m 03s')->format('H:i:s');
?>
以上例程的输出类似于:
23:15:03
参见
- DateTime::__construct
- DateTime::getLastErrors
- checkdate
- strptime
DateTime::createFromImmutable
Returns new DateTime object encapsulating the given DateTimeImmutable object
说明
public <span
class="modifier">static DateTime <span
class="methodname">DateTime::createFromImmutable ( <span
class="methodparam">DateTimeImmutable
$object
)
参数
object
The immutable DateTimeImmutable object
that needs to be converted to a mutable version. This object is not
modified, but instead a new DateTime
object is created containing the same date, time, and timezone
information.
范例
示例 #1 Creating a mutable date time object
<?php
$date = new DateTimeImmutable("2014-06-20 11:45 Europe/London");
$mutable = DateTime::createFromImmutable( $date );
?>
返回值
Returns a new DateTime instance.
DateTime::createFromInterface
Returns new DateTime object encapsulating the given DateTimeInterface object
说明
public <span
class="modifier">static DateTime <span
class="methodname">DateTime::createFromInterface ( <span
class="methodparam">DateTimeInterface
$object
)
参数
object
The DateTimeInterface object that needs
to be converted to a mutable version. This object is not modified, but
instead a new DateTime object is created
containing the same date, time, and timezone information.
返回值
Returns a new DateTime instance.
范例
示例 #1 Creating a mutable date time object
<?php
$date = new DateTimeImmutable("2014-06-20 11:45 Europe/London");
$mutable = DateTime::createFromInterface($date);
$date = new DateTime("2014-06-20 11:45 Europe/London");
$also_mutable = DateTime::createFromInterface($date);
?>
DateTime::getLastErrors
date_get_last_errors
获取警告和错误信息
说明
面向对象风格
public <span class="modifier">static array <span class="methodname">DateTime::getLastErrors ( <span class="methodparam">void )
过程化风格
array <span class="methodname">date_get_last_errors ( <span class="methodparam">void )
返回在解析日期时间字符串的过程中发生的警告和错误信息。
参数
此函数没有参数。
返回值
返回一个数组,其中包含在解析日期时间字符串的过程中发生的警告和错误信息。
范例
示例 #1 DateTime::getLastErrors 例程
面向对象风格
<?php
try {
$date = new DateTime('asdfasdf');
} catch (Exception $e) {
// 仅出于演示的目的...
print_r(DateTime::getLastErrors());
// 实际的代码中你应该这样使用返回对象
// echo $e->getMessage();
}
?>
过程化风格
<?php
$date = date_create('asdfasdf');
print_r(date_get_last_errors());
?>
以上例程会输出:
Array
(
[warning_count] => 1
[warnings] => Array
(
[6] => Double timezone specification
)
[error_count] => 1
[errors] => Array
(
[0] => The timezone could not be found in the database
)
)
返回数组中的索引 6 和 0 表示在解析过程中,所提供的日期时间字符串中无法正确解析的字符位置。
DateTime::modify
date_modify
修改日期时间对象的值
说明
面向对象风格
public DateTime
DateTime::modify ( <span
class="methodparam">string $modifier
)
过程化风格
DateTime <span
class="methodname">date_modify ( <span
class="type">DateTime $object
, <span
class="methodparam">string $modifier
)
修改一个日期时间对象的值。 支持 <span class="function">DateTimeImmutable::__construct 函数所允许的字符串。
参数
object
仅过程化风格:由 date_create 返回的 <span
class="classname">DateTime 类型的对象。此函数会修改这个对象。
modifier
日期/时间字符串。正确格式的说明详见
日期与时间格式。
返回值
返回被修改的 DateTime 对象, 或者在失败时返回 false
.
范例
示例 #1 DateTime::modify 例程
面向对象风格
<?php
$date = new DateTime('2006-12-12');
$date->modify('+1 day');
echo $date->format('Y-m-d');
?>
过程化风格
<?php
$date = date_create('2006-12-12');
date_modify($date, '+1 day');
echo date_format($date, 'Y-m-d');
?>
以上例程会输出:
2006-12-13
示例 #2 增加或者减少月份的时候需要注意
<?php
$date = new DateTime('2000-12-31');
$date->modify('+1 month');
echo $date->format('Y-m-d') . "\n";
$date->modify('+1 month');
echo $date->format('Y-m-d') . "\n";
?>
以上例程会输出:
2001-01-31
2001-03-03
参见
- strtotime
- DateTime::add
- DateTime::sub
- DateTime::setDate
- DateTime::setISODate
- DateTime::setTime
- DateTime::setTimestamp
DateTime::__set_state
__set_state 魔术方法处理函数
说明
public <span
class="modifier">static DateTime <span
class="methodname">DateTime::__set_state ( <span
class="methodparam">array $array
)
__set_state() 魔术方法处理函数。
参数
array
用来初始化对象属性值的数组。
返回值
返回 DateTime 对象实例。
DateTime::setDate
date_date_set
设置 DateTime 对象的日期
说明
面向对象风格
public DateTime
DateTime::setDate ( <span
class="methodparam">int $year
, <span
class="methodparam">int $month
,
int $day
)
过程化风格
DateTime <span
class="methodname">date_date_set ( <span
class="methodparam">DateTime $object
, int $year
, int
$month
, <span
class="type">int $day
)
设置 DateTime 对象的日期。
参数
object
仅过程化风格:由 date_create 返回的 <span
class="classname">DateTime 类型的对象。此函数会修改这个对象。
year
年份。
month
月份。
day
日。
返回值
返回被修改的 DateTime 对象, 或者在失败时返回 false
.
范例
示例 #1 DateTime::setDate 例程
面向对象风格
<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d');
?>
过程化风格
<?php
$date = date_create();
date_date_set($date, 2001, 2, 3);
echo date_format($date, 'Y-m-d');
?>
以上例程会输出:
2001-02-03
示例 #2 超出范围的部分会向上一级增加
<?php
$date = new DateTime();
$date->setDate(2001, 2, 28);
echo $date->format('Y-m-d') . "\n";
$date->setDate(2001, 2, 29);
echo $date->format('Y-m-d') . "\n";
$date->setDate(2001, 14, 3);
echo $date->format('Y-m-d') . "\n";
?>
以上例程会输出:
2001-02-28
2001-03-01
2002-02-03
参见
- DateTime::setISODate
- DateTime::setTime
DateTime::setISODate
date_isodate_set
设置 ISO 日期
说明
面向对象风格
public DateTime
DateTime::setISODate ( <span
class="methodparam">int $year
, <span
class="methodparam">int $week
[,
int
$dayOfWeek
= 1 ] )
过程化风格
DateTime <span
class="methodname">date_isodate_set ( <span
class="methodparam">DateTime $object
, int $year
, int $week
[, int
$dayOfWeek
= 1 ] )
以 ISO 8601 规范的格式设置日期, 使用周和日的偏移量作为参数,而不是使用月和日。
参数
object
仅过程化风格:由 date_create 返回的 <span
class="classname">DateTime 类型的对象。此函数会修改这个对象。
year
年份。
week
周。
dayOfWeek
从周的第一天计算,日在一周内的偏移量。
返回值
返回被修改的 DateTime 对象, 或者在失败时返回 false
.
范例
示例 #1 DateTime::setISODate 例程
面向对象风格
<?php
$date = new DateTime();
$date->setISODate(2008, 2);
echo $date->format('Y-m-d') . "\n";
$date->setISODate(2008, 2, 7);
echo $date->format('Y-m-d') . "\n";
?>
过程化风格
<?php
$date = date_create();
date_isodate_set($date, 2008, 2);
echo date_format($date, 'Y-m-d') . "\n";
date_isodate_set($date, 2008, 2, 7);
echo date_format($date, 'Y-m-d') . "\n";
?>
以上例程会输出:
2008-01-07
2008-01-13
示例 #2 超出有效范围的部分,会加到上一级
<?php
$date = new DateTime();
$date->setISODate(2008, 2, 7);
echo $date->format('Y-m-d') . "\n";
$date->setISODate(2008, 2, 8);
echo $date->format('Y-m-d') . "\n";
$date->setISODate(2008, 53, 7);
echo $date->format('Y-m-d') . "\n";
?>
以上例程会输出:
2008-01-13
2008-01-14
2009-01-04
示例 #3 找出来某个周所属的月份
<?php
$date = new DateTime();
$date->setISODate(2008, 14);
echo $date->format('n');
?>
以上例程会输出:
3
参见
- DateTime::setDate
- DateTime::setTime
DateTime::setTime
date_time_set
设置 DateTime 对象的时间
说明
面向对象风格
public DateTime
DateTime::setTime ( <span
class="methodparam">int $hour
, <span
class="methodparam">int $minute
[,
int $second
<span
class="initializer"> = 0 [, <span
class="methodparam">int $microsecond
<span
class="initializer"> = 0 ]] )
过程化风格
DateTime <span
class="methodname">date_time_set ( <span
class="methodparam">DateTime $object
, int $hour
, int
$minute
[, <span
class="type">int $second
=
0 [, <span
class="type">int $microsecond
=
0 ]] )
设置 DateTime 对象的时间。
参数
object
仅过程化风格:由 date_create 返回的 <span
class="classname">DateTime 类型的对象。此函数会修改这个对象。
hour
小时。
minute
分钟。
second
秒。
microsecond
微秒。
返回值
返回被修改的 DateTime 对象, 或者在失败时返回 false
.
更新日志
版本 | 说明 |
---|---|
7.1.0 | 新增 microsecond 参数。 |
范例
示例 #1 DateTime::setTime 例程
面向对象风格
<?php
$date = new DateTime('2001-01-01');
$date->setTime(14, 55);
echo $date->format('Y-m-d H:i:s') . "\n";
$date->setTime(14, 55, 24);
echo $date->format('Y-m-d H:i:s') . "\n";
?>
过程化风格
<?php
$date = date_create('2001-01-01');
date_time_set($date, 14, 55);
echo date_format($date, 'Y-m-d H:i:s') . "\n";
date_time_set($date, 14, 55, 24);
echo date_format($date, 'Y-m-d H:i:s') . "\n";
?>
以上例程的输出类似于:
2001-01-01 14:55:00
2001-01-01 14:55:24
示例 #2 超出有效范围的部分会增加到上一级
<?php
$date = new DateTime('2001-01-01');
$date->setTime(14, 55, 24);
echo $date->format('Y-m-d H:i:s') . "\n";
$date->setTime(14, 55, 65);
echo $date->format('Y-m-d H:i:s') . "\n";
$date->setTime(14, 65, 24);
echo $date->format('Y-m-d H:i:s') . "\n";
$date->setTime(25, 55, 24);
echo $date->format('Y-m-d H:i:s') . "\n";
?>
以上例程会输出:
2001-01-01 14:55:24
2001-01-01 14:56:05
2001-01-01 15:05:24
2001-01-02 01:55:24
参见
- DateTime::setDate
- DateTime::setISODate
DateTime::setTimestamp
date_timestamp_set
以 Unix 时间戳的方式设置 DateTime 对象
说明
面向对象风格
public DateTime
DateTime::setTimestamp ( <span
class="methodparam">int
$unixtimestamp
)
过程化风格
DateTime <span
class="methodname">date_timestamp_set ( <span
class="methodparam">DateTime $object
, int
$unixtimestamp
)
以 Unix 时间戳的方式设置 DateTime 对象的日期和时间。
参数
object
仅过程化风格:由 date_create 返回的 <span
class="classname">DateTime 类型的对象。此函数会修改这个对象。
unixtimestamp
表示日期时间的 Unix 时间戳。
返回值
返回被修改的 DateTime 对象, 或者在失败时返回 false
.
范例
示例 #1 DateTime::setTimestamp 例程
面向对象风格
<?php
$date = new DateTime();
echo $date->format('U = Y-m-d H:i:s') . "\n";
$date->setTimestamp(1171502725);
echo $date->format('U = Y-m-d H:i:s') . "\n";
?>
过程化风格
<?php
$date = date_create();
echo date_format($date, 'U = Y-m-d H:i:s') . "\n";
date_timestamp_set($date, 1171502725);
echo date_format($date, 'U = Y-m-d H:i:s') . "\n";
?>
以上例程的输出类似于:
1272508903 = 2010-04-28 22:41:43
1171502725 = 2007-02-14 20:25:25
注释
PHP 5.2 之后的版本中, 可以使用 Unix 时间戳作为构造函数的参数生成新的 DateTime 对象,例如:
示例 #2 PHP 5.2 之后的版本中,<span class="function">DateTime::setTimestamp 替代方案
<?php
$ts = 1171502725;
$date = new DateTime("@$ts");
echo $date->format('U = Y-m-d H:i:s') . "\n";
?>
以上例程的输出类似于:
1171502725 = 2007-02-14 20:25:25
参见
- DateTime::getTimestamp
DateTime::setTimezone
date_timezone_set
设置 DateTime 对象的时区
说明
面向对象风格
public DateTime
DateTime::setTimezone ( <span
class="methodparam">DateTimeZone
$timezone
)
过程化风格
DateTime <span
class="methodname">date_timezone_set ( <span
class="methodparam">DateTime $object
, DateTimeZone
$timezone
)
设置 DateTime <span class="type">object 的时区。
参数
object
仅过程化风格:由 date_create 返回的 <span
class="classname">DateTime 类型的对象。此函数会修改这个对象。
timezone
DateTimeZone 对象, 表示要设置为时区。
返回值
返回被修改的 DateTime 对象, 或者在失败时返回 false
.
范例
示例 #1 DateTime::setTimeZone 例程
面向对象风格
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
过程化风格
<?php
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
?>
以上例程会输出:
2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45
参见
- DateTime::getTimezone
- DateTimeZone::__construct
DateTime::sub
date_sub
对一个 DateTime 对象减去一定量的 日、月、年、小时、分钟和秒。
说明
面向对象风格
public DateTime
DateTime::sub ( <span
class="methodparam">DateInterval
$interval
)
过程化风格
DateTime <span
class="methodname">date_sub ( <span
class="type">DateTime $object
, <span
class="methodparam">DateInterval
$interval
)
DateInterval 对象, 表示要减去多少量的时间。
参数
object
仅过程化风格:由 date_create 返回的 <span
class="classname">DateTime 类型的对象。此函数会修改这个对象。
interval
DateInterval 对象
返回值
返回被修改的 DateTime 对象, 或者在失败时返回 false
.
范例
示例 #1 DateTime::sub 例程
面向对象风格
<?php
$date = new DateTime('2000-01-20');
$date->sub(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>
过程化风格
<?php
$date = date_create('2000-01-20');
date_sub($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');
?>
以上例程会输出:
2000-01-10
示例 #2 Further DateTime::sub 例程
<?php
$date = new DateTime('2000-01-20');
$date->sub(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
$date = new DateTime('2000-01-20');
$date->sub(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
以上例程会输出:
2000-01-19 13:59:30
1992-08-15 19:56:58
示例 #3 减去一定量的月份的时候需要注意
<?php
$date = new DateTime('2001-04-30');
$interval = new DateInterval('P1M');
$date->sub($interval);
echo $date->format('Y-m-d') . "\n";
$date->sub($interval);
echo $date->format('Y-m-d') . "\n";
?>
以上例程会输出:
2001-03-30
2001-03-02
注释
在 PHP 5.2 之后,可以使用 DateTime::modify 作为替代。
参见
- DateTime::add
- DateTime::diff
- DateTime::modify
简介
Representation of date and time.
类摘要
DateTimeImmutable
class DateTimeImmutable <span class="oointerface">implements <span class="interfacename">DateTimeInterface {
/* 继承的常量 */
const string
DateTimeInterface::ATOM
=
"Y-m-d\TH:i:sP" ;
const string
DateTimeInterface::COOKIE
= "l, d-M-Y H:i:s
T" ;
const string
DateTimeInterface::ISO8601
=
"Y-m-d\TH:i:sO" ;
const string
DateTimeInterface::RFC822
= "D, d M y H:i:s
O" ;
const string
DateTimeInterface::RFC850
= "l, d-M-y H:i:s
T" ;
const string
DateTimeInterface::RFC1036
= "D, d M y
H:i:s O" ;
const string
DateTimeInterface::RFC1123
= "D, d M Y
H:i:s O" ;
const string
DateTimeInterface::RFC7231
= "D, d M Y
H:i:s \G\M\T" ;
const string
DateTimeInterface::RFC2822
= "D, d M Y
H:i:s O" ;
const string
DateTimeInterface::RFC3339
=
"Y-m-d\TH:i:sP" ;
const string
DateTimeInterface::RFC3339_EXTENDED
=
"Y-m-d\TH:i:s.vP" ;
const string
DateTimeInterface::RSS
= "D, d M Y H:i:s
O" ;
const string
DateTimeInterface::W3C
=
"Y-m-d\TH:i:sP" ;
/* 方法 */
public <span
class="methodname">__construct ([ <span
class="methodparam">string $datetime
<span
class="initializer"> = "now" [, <span
class="methodparam"><span
class="type">DateTimeZonenull
$timezone
= null
]] )
public <span
class="type">DateTimeImmutable <span
class="methodname">add ( <span
class="type">DateInterval $interval
)
public <span
class="modifier">static <span
class="type">DateTimeImmutable<span
class="type">false <span
class="methodname">createFromFormat ( <span
class="methodparam">string $format
,
string
$datetime
[, <span
class="type">DateTimeZone<span
class="type">null $timezone
=
null
] )
public <span
class="modifier">static <span
class="type">DateTimeImmutable <span
class="methodname">createFromInterface ( <span
class="methodparam">DateTimeInterface
$object
)
public <span
class="modifier">static <span
class="type">DateTimeImmutable <span
class="methodname">createFromMutable ( <span
class="methodparam">DateTime $object
)
public <span class="modifier">static <span class="type">arrayfalse <span class="methodname">getLastErrors ( <span class="methodparam">void )
public <span
class="type">DateTimeImmutable<span
class="type">false modify
( string
$modifier
)
public <span
class="modifier">static <span
class="type">DateTimeImmutable <span
class="methodname">__set_state ( <span
class="methodparam">array $array
)
public <span
class="type">DateTimeImmutable <span
class="methodname">setDate ( <span
class="type">int $year
, <span
class="type">int $month
, <span
class="methodparam">int $day
)
public <span
class="type">DateTimeImmutable <span
class="methodname">setISODate ( <span
class="type">int $year
, <span
class="type">int $week
[, <span
class="methodparam">int $dayOfWeek
<span
class="initializer"> = 1 ] )
public <span
class="type">DateTimeImmutable <span
class="methodname">setTime ( <span
class="type">int $hour
, <span
class="type">int $minute
[, <span
class="methodparam">int $second
<span
class="initializer"> = 0 [, <span
class="methodparam">int $microsecond
<span
class="initializer"> = 0 ]] )
public <span
class="type">DateTimeImmutable <span
class="methodname">setTimestamp ( <span
class="type">int $timestamp
)
public <span
class="type">DateTimeImmutable <span
class="methodname">setTimezone ( <span
class="type">DateTimeZone $timezone
)
public <span
class="type">DateTimeImmutable <span
class="methodname">sub ( <span
class="type">DateInterval $interval
)
public <span
class="type">DateIntervalfalse
diff ( <span
class="type">DateTimeInterface $targetObject
[, <span
class="methodparam">bool $absolute
<span
class="initializer"> = false
] )
public string
format ( <span
class="type">string $format
)
publicint <span class="methodname">getOffset ( <span class="methodparam">void )
public int <span class="methodname">getTimestamp ( <span class="methodparam">void )
public <span class="type">DateTimeZonefalse getTimezone ( <span class="methodparam">void )
public void __wakeup ( <span class="methodparam">void )
}
DateTimeImmutable::add
Adds an amount of days, months, years, hours, minutes and seconds
说明
public <span
class="type">DateTimeImmutable <span
class="methodname">DateTimeImmutable::add ( <span
class="methodparam">DateInterval
$interval
)
Like DateTime::add but works with <span class="classname">DateTimeImmutable.
DateTimeImmutable::__construct
date_create_immutable
Returns new DateTimeImmutable object
说明
面向对象风格
public <span
class="methodname">DateTimeImmutable::__construct ([ <span
class="methodparam">string $datetime
<span
class="initializer"> = "now" [, <span
class="methodparam"><span
class="type">DateTimeZonenull
$timezone
= null
]] )
过程化风格
DateTimeImmutable<span
class="type">false <span
class="methodname">date_create_immutable ([ <span
class="methodparam">string $datetime
<span
class="initializer"> = "now" [, <span
class="methodparam"><span
class="type">DateTimeZonenull
$timezone
= null
]] )
Like DateTime::__construct but works with DateTimeImmutable.
DateTimeImmutable::createFromFormat
date_create_immutable_from_format
Parses a time string according to a specified format
说明
面向对象风格
public <span
class="modifier">static <span
class="type">DateTimeImmutable<span
class="type">false <span
class="methodname">DateTimeImmutable::createFromFormat ( <span
class="methodparam">string $format
,
string
$datetime
[, <span
class="type">DateTimeZone<span
class="type">null $timezone
=
null
] )
过程化风格
DateTimeImmutable<span
class="type">false <span
class="methodname">date_create_immutable_from_format ( <span
class="methodparam">string $format
,
string
$datetime
[, <span
class="type">DateTimeZone<span
class="type">null $timezone
=
null
] )
Like DateTime::createFromFormat but works with DateTimeImmutable.
DateTimeImmutable::createFromInterface
Returns new DateTimeImmutable object encapsulating the given DateTimeInterface object
说明
public <span
class="modifier">static <span
class="type">DateTimeImmutable <span
class="methodname">DateTimeImmutable::createFromInterface ( <span
class="methodparam">DateTimeInterface
$object
)
参数
object
The DateTimeInterface object that needs
to be converted to an immutable version. This object is not modified,
but instead a new DateTimeImmutable
object is created containing the same date, time, and timezone
information.
返回值
Returns a new DateTimeImmutable instance.
范例
示例 #1 Creating an immutable date time object
<?php
$date = new DateTime("2014-06-20 11:45 Europe/London");
$immutable = DateTimeImmutable::createFromInterface($date);
$date = new DateTimeImmutable("2014-06-20 11:45 Europe/London");
$also_immutable = DateTimeImmutable::createFromInterface($date);
?>
DateTimeImmutable::createFromMutable
Returns new DateTimeImmutable object encapsulating the given DateTime object
说明
public <span
class="modifier">static <span
class="type">DateTimeImmutable <span
class="methodname">DateTimeImmutable::createFromMutable ( <span
class="methodparam">DateTime $object
)
参数
object
The mutable DateTime object that you want
to convert to an immutable version. This object is not modified, but
instead a new DateTimeImmutable object is
created containing the same date time and timezone information.
范例
示例 #1 Creating an immutable date time object
<?php
$date = new DateTime("2014-06-20 11:45 Europe/London");
$immutable = DateTimeImmutable::createFromMutable( $date );
?>
返回值
Returns a new DateTimeImmutable instance.
DateTimeImmutable::getLastErrors
Returns the warnings and errors
说明
public <span class="modifier">static <span class="type">arrayfalse <span class="methodname">DateTimeImmutable::getLastErrors ( <span class="methodparam">void )
Like DateTime::getLastErrors but works with DateTimeImmutable.
DateTimeImmutable::modify
Creates a new object with modified timestamp
说明
public <span
class="type">DateTimeImmutable<span
class="type">false <span
class="methodname">DateTimeImmutable::modify ( <span
class="methodparam">string $modifier
)
Creates a new DateTimeImmutable object with modified timestamp. The original object is not modified.
参数
object
仅过程化风格:由 date_create 返回的 <span
class="classname">DateTime 类型的对象。此函数会修改这个对象。
modifier
日期/时间字符串。正确格式的说明详见
日期与时间格式。
返回值
Returns the newly created object 或者在失败时返回 false
.
DateTimeImmutable::__set_state
The __set_state handler
说明
public <span
class="modifier">static <span
class="type">DateTimeImmutable <span
class="methodname">DateTimeImmutable::__set_state ( <span
class="methodparam">array $array
)
Like DateTime::__set_state but works with DateTimeImmutable.
DateTimeImmutable::setDate
Sets the date
说明
public <span
class="type">DateTimeImmutable <span
class="methodname">DateTimeImmutable::setDate ( <span
class="methodparam">int $year
, <span
class="methodparam">int $month
,
int $day
)
Like DateTime::setDate but works with DateTimeImmutable.
DateTimeImmutable::setISODate
Sets the ISO date
说明
public <span
class="type">DateTimeImmutable <span
class="methodname">DateTimeImmutable::setISODate ( <span
class="methodparam">int $year
, <span
class="methodparam">int $week
[,
int
$dayOfWeek
= 1 ] )
Like DateTime::setISODate but works with DateTimeImmutable.
DateTimeImmutable::setTime
Sets the time
说明
public <span
class="type">DateTimeImmutable <span
class="methodname">DateTimeImmutable::setTime ( <span
class="methodparam">int $hour
, <span
class="methodparam">int $minute
[,
int $second
<span
class="initializer"> = 0 [, <span
class="methodparam">int $microsecond
<span
class="initializer"> = 0 ]] )
Like DateTime::setTime but works with DateTimeImmutable.
DateTimeImmutable::setTimestamp
Sets the date and time based on a Unix timestamp
说明
public <span
class="type">DateTimeImmutable <span
class="methodname">DateTimeImmutable::setTimestamp ( <span
class="methodparam">int $timestamp
)
Like DateTime::setTimestamp but works with DateTimeImmutable.
DateTimeImmutable::setTimezone
Sets the time zone
说明
public <span
class="type">DateTimeImmutable <span
class="methodname">DateTimeImmutable::setTimezone ( <span
class="methodparam">DateTimeZone
$timezone
)
Like DateTime::setTimezone but works with DateTimeImmutable.
DateTimeImmutable::sub
Subtracts an amount of days, months, years, hours, minutes and seconds
说明
public <span
class="type">DateTimeImmutable <span
class="methodname">DateTimeImmutable::sub ( <span
class="methodparam">DateInterval
$interval
)
Like DateTime::sub but works with <span class="classname">DateTimeImmutable.
简介
DateTimeInterface 的意思是可以约束类型为 DateTime 或 DateTimeImmutable。此接口不能让用户自己的 class 去实现(implements)。
类摘要
DateTimeInterface
class DateTimeInterface {
/* 常量 */
const string
DateTimeInterface::ATOM
=
"Y-m-d\TH:i:sP" ;
const string
DateTimeInterface::COOKIE
= "l, d-M-Y H:i:s
T" ;
const string
DateTimeInterface::ISO8601
=
"Y-m-d\TH:i:sO" ;
const string
DateTimeInterface::RFC822
= "D, d M y H:i:s
O" ;
const string
DateTimeInterface::RFC850
= "l, d-M-y H:i:s
T" ;
const string
DateTimeInterface::RFC1036
= "D, d M y
H:i:s O" ;
const string
DateTimeInterface::RFC1123
= "D, d M Y
H:i:s O" ;
const string
DateTimeInterface::RFC7231
= "D, d M Y
H:i:s \G\M\T" ;
const string
DateTimeInterface::RFC2822
= "D, d M Y
H:i:s O" ;
const string
DateTimeInterface::RFC3339
=
"Y-m-d\TH:i:sP" ;
const string
DateTimeInterface::RFC3339_EXTENDED
=
"Y-m-d\TH:i:s.vP" ;
const string
DateTimeInterface::RSS
= "D, d M Y H:i:s
O" ;
const string
DateTimeInterface::W3C
=
"Y-m-d\TH:i:sP" ;
/* 方法 */
public <span
class="type">DateIntervalfalse
diff ( <span
class="type">DateTimeInterface $targetObject
[, <span
class="methodparam">bool $absolute
<span
class="initializer"> = false
] )
public string
format ( <span
class="type">string $format
)
publicint <span class="methodname">getOffset ( <span class="methodparam">void )
public int <span class="methodname">getTimestamp ( <span class="methodparam">void )
public <span class="type">DateTimeZonefalse getTimezone ( <span class="methodparam">void )
public void __wakeup ( <span class="methodparam">void )
}
预定义常量
DateTimeInterface::ATOM
DATE_ATOM
Atom 格式(示例:2005-08-15T15:52:01+00:00)
DateTimeInterface::COOKIE
DATE_COOKIE
HTTP Cookies 格式(示例:Monday, 15-Aug-2005
15:52:01 UTC)
DateTimeInterface::ISO8601
DATE_ISO8601
ISO-8601 格式(示例:2005-08-15T15:52:01+0000)
Note: 这种格式和 ISO-8601 的格式并不兼容,只是出于向后兼容的原因才保留的。 如果要使用和 ISO-8601 兼容的格式, 请使用
DateTime::ATOM
或DATE_ATOM
两个常量。
DateTimeInterface::RFC822
DATE_RFC822
RFC 822 格式(示例:Mon, 15 Aug 05 15:52:01
+0000)
DateTimeInterface::RFC850
DATE_RFC850
RFC 850 格式(示例:Monday, 15-Aug-05 15:52:01
UTC)
DateTimeInterface::RFC1036
DATE_RFC1036
RFC 1036(示例:Mon, 15 Aug 05 15:52:01 +0000)
DateTimeInterface::RFC1123
DATE_RFC1123
RFC 1123 格式(示例:Mon, 15 Aug 2005 15:52:01
+0000)
DateTimeInterface::RFC7231
DATE_RFC7231
RFC 7231 格式 (自 PHP 7.0.19 和 7.1.5 可用)
(示例:Sat, 30 Apr 2016 17:52:13 GMT)
DateTimeInterface::RFC2822
DATE_RFC2822
RFC 2822 格式(示例:Mon, 15 Aug 2005 15:52:01
+0000)
DateTimeInterface::RFC3339
DATE_RFC3339
同 DATE_ATOM
(自 PHP 5.1.3 版本可用)
DateTimeInterface::RFC3339_EXTENDED
DATE_RFC3339_EXTENDED
RFC 3339 EXTENDED 格式(自 PHP 7.0.0
可用)(示例:2005-08-15T15:52:01.000+00:00)
DateTimeInterface::RSS
DATE_RSS
RSS(示例:Mon, 15 Aug 2005 15:52:01 +0000)
DateTimeInterface::W3C
DATE_W3C
RSS 格式(示例:2005-08-15T15:52:01+00:00)
更新日志
版本 | 说明 |
---|---|
7.2.0 | DateTime 的类常量现在定义在了 DateTimeInterface 上。 |
5.5.8 | 尝试实现(implement)DateTimeInterface 时,会抛出严重错误。 之前此举不会产生错误,但这种做法是不对的。 |
DateTime::diff
DateTimeImmutable::diff
DateTimeInterface::diff
date_diff
Returns the difference between two DateTime objects
说明
面向对象风格
public <span
class="type">DateIntervalfalse
DateTime::diff ( <span
class="methodparam">DateTimeInterface
$targetObject
[, <span
class="type">bool $absolute
=
false
] )
public <span
class="type">DateIntervalfalse
DateTimeImmutable::diff ( <span
class="methodparam">DateTimeInterface
$targetObject
[, <span
class="type">bool $absolute
=
false
] )
public <span
class="type">DateIntervalfalse
DateTimeInterface::diff ( <span
class="methodparam">DateTimeInterface
$targetObject
[, <span
class="type">bool $absolute
=
false
] )
过程化风格
DateInterval <span
class="methodname">date_diff ( <span
class="type">DateTimeInterface $baseObject
, <span
class="methodparam">DateTimeInterface
$targetObject
[, <span
class="type">bool $absolute
=
false
] )
Returns the difference between two <span class="classname">DateTimeInterface objects.
参数
datetime
The date to compare to.
absolute
Should the interval be forced to be positive?
返回值
The DateInterval object represents the
difference between the two dates 或者在失败时返回 false
.
The return value more specifically represents the interval to apply to
the original object ($this
or $originObject
) to arrive at the
$targetObject
. This process is not always reversible.
范例
示例 #1 DateTime::diff example
面向对象风格
<?php
$origin = new DateTime('2009-10-11');
$target = new DateTime('2009-10-13');
$interval = $origin->diff($target);
echo $interval->format('%R%a days');
?>
过程化风格
<?php
$origin = date_create('2009-10-11');
$target = date_create('2009-10-13');
$interval = date_diff($origin, $target);
echo $interval->format('%R%a days');
?>
以上例程会输出:
+2 days
示例 #2 DateTime object comparison
Note:
As of PHP 5.2.2, DateTime objects can be compared using comparison operators.
<?php
$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");
var_dump($date1 == $date2);
var_dump($date1 < $date2);
var_dump($date1 > $date2);
?>
以上例程会输出:
bool(false)
bool(true)
bool(false)
参见
- DateInterval::format
- DateTime::add
- DateTime::sub
DateTime::format
DateTimeImmutable::format
DateTimeInterface::format
date_format
Returns date formatted according to given format
说明
面向对象风格
public string
DateTime::format ( <span
class="methodparam">string $format
)
public string
DateTimeImmutable::format ( <span
class="methodparam">string $format
)
public string
DateTimeInterface::format ( <span
class="methodparam">string $format
)
过程化风格
string <span
class="methodname">date_format ( <span
class="type">DateTimeInterface $object
, <span
class="methodparam">string $format
)
Returns date formatted according to given format.
参数
object
仅为过程化风格:由 date_create 返回的
DateTime 类型的对象。
format
The format of the outputted date string. See
the formatting options below. There are also several
predefined date constants
that may be used instead, so for example DATE_RSS
contains the
format string 'D, d M Y H:i:s'.
format character |
Description | Example returned values |
---|---|---|
Day | --- | --- |
d | Day of the month, 2 digits with leading zeros | 01 to 31 |
D | A textual representation of a day, three letters | Mon through Sun |
j | Day of the month without leading zeros | 1 to 31 |
l (lowercase 'L') | A full textual representation of the day of the week | Sunday through Saturday |
N | ISO-8601 numeric representation of the day of the week | 1 (for Monday) through 7 (for Sunday) |
S | English ordinal suffix for the day of the month, 2 characters | st, nd, rd or th. Works well with j |
w | Numeric representation of the day of the week | 0 (for Sunday) through 6 (for Saturday) |
z | The day of the year (starting from 0) | 0 through 365 |
Week | --- | --- |
W | ISO-8601 week number of year, weeks starting on Monday | Example: 42 (the 42nd week in the year) |
Month | --- | --- |
F | A full textual representation of a month, such as January or March | January through December |
m | Numeric representation of a month, with leading zeros | 01 through 12 |
M | A short textual representation of a month, three letters | Jan through Dec |
n | Numeric representation of a month, without leading zeros | 1 through 12 |
t | Number of days in the given month | 28 through 31 |
Year | --- | --- |
L | Whether it's a leap year | 1 if it is a leap year, 0 otherwise. |
o | ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. | Examples: 1999 or 2003 |
Y | A full numeric representation of a year, 4 digits | Examples: 1999 or 2003 |
y | A two digit representation of a year | Examples: 99 or 03 |
Time | --- | --- |
a | Lowercase Ante meridiem and Post meridiem | am or pm |
A | Uppercase Ante meridiem and Post meridiem | AM or PM |
B | Swatch Internet time | 000 through 999 |
g | 12-hour format of an hour without leading zeros | 1 through 12 |
G | 24-hour format of an hour without leading zeros | 0 through 23 |
h | 12-hour format of an hour with leading zeros | 01 through 12 |
H | 24-hour format of an hour with leading zeros | 00 through 23 |
i | Minutes with leading zeros | 00 to 59 |
s | Seconds with leading zeros | 00 through 59 |
u | Microseconds. Note that date will always generate 000000 since it takes an int parameter, whereas DateTime::format does support microseconds if DateTime was created with microseconds. | Example: 654321 |
v | Milliseconds (added in PHP 7.0.0). Same note applies as for u. | Example: 654 |
Timezone | --- | --- |
e | Timezone identifier | Examples: UTC, GMT, Atlantic/Azores |
I (capital i) | Whether or not the date is in daylight saving time | 1 if Daylight Saving Time, 0 otherwise. |
O | Difference to Greenwich time (GMT) without colon between hours and minutes | Example: +0200 |
P | Difference to Greenwich time (GMT) with colon between hours and minutes | Example: +02:00 |
p | The same as P, but returns Z instead of +00:00 | Example: +02:00 |
T | Timezone abbreviation | Examples: EST, MDT ... |
Z | Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. | -43200 through 50400 |
Full Date/Time | --- | --- |
c | ISO 8601 date | 2004-02-12T15:19:21+00:00 |
r | » RFC 2822 formatted date | Example: Thu, 21 Dec 2000 16:01:07 +0200 |
U | Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) | See also time |
Unrecognized characters in the format string will be printed as-is. The Z format will always return 0 when using <span class="function">gmdate.
Note:
Since this function only accepts int timestamps the u format character is only useful when using the date_format function with user based timestamps created with date_create.
返回值
Returns the formatted date string on success.
更新日志
版本 | 说明 |
---|---|
8.0.0 | Prior to this version, false was returned on failure. |
范例
示例 #1 DateTime::format example
面向对象风格
<?php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
?>
过程化风格
<?php
$date = date_create('2000-01-01');
echo date_format($date, 'Y-m-d H:i:s');
?>
以上例程会输出:
2000-01-01 00:00:00
注释
This method does not use locales. All output is in English.
参见
- date
DateTime::getOffset
DateTimeImmutable::getOffset
DateTimeInterface::getOffset
date_offset_get
Returns the timezone offset
说明
面向对象风格
publicint <span class="methodname">DateTime::getOffset ( <span class="methodparam">void )
publicint <span class="methodname">DateTimeImmutable::getOffset ( <span class="methodparam">void )
publicint <span class="methodname">DateTimeInterface::getOffset ( <span class="methodparam">void )
过程化风格
int <span
class="methodname">date_offset_get ( <span
class="methodparam">DateTimeInterface
$object
)
Returns the timezone offset.
参数
object
仅为过程化风格:由 date_create 返回的
DateTime 类型的对象。
返回值
Returns the timezone offset in seconds from UTC on success.
更新日志
版本 | 说明 |
---|---|
8.0.0 | Prior to this version, false was returned on failure. |
范例
示例 #1 DateTime::getOffset example
面向对象风格
<?php
$winter = new DateTime('2010-12-21', new DateTimeZone('America/New_York'));
$summer = new DateTime('2008-06-21', new DateTimeZone('America/New_York'));
echo $winter->getOffset() . "\n";
echo $summer->getOffset() . "\n";
?>
过程化风格
<?php
$winter = date_create('2010-12-21', timezone_open('America/New_York'));
$summer = date_create('2008-06-21', timezone_open('America/New_York'));
echo date_offset_get($winter) . "\n";
echo date_offset_get($summer) . "\n";
?>
以上例程会输出:
-18000
-14400
Note: -18000 = -5 hours, -14400 = -4 hours.
DateTime::getTimestamp
DateTimeImmutable::getTimestamp
DateTimeInterface::getTimestamp
date_timestamp_get
Gets the Unix timestamp
说明
面向对象风格
public int <span class="methodname">DateTime::getTimestamp ( <span class="methodparam">void )
public int <span class="methodname">DateTimeImmutable::getTimestamp ( <span class="methodparam">void )
public int <span class="methodname">DateTimeInterface::getTimestamp ( <span class="methodparam">void )
过程化风格
int <span
class="methodname">date_timestamp_get ( <span
class="methodparam">DateTimeInterface
$object
)
Gets the Unix timestamp.
参数
此函数没有参数。
返回值
Returns the Unix timestamp representing the date.
更新日志
版本 | 说明 |
---|---|
8.0.0 | These functions no longer return false on failure. |
范例
示例 #1 DateTime::getTimestamp example
面向对象风格
<?php
$date = new DateTime();
echo $date->getTimestamp();
?>
过程化风格
<?php
$date = date_create();
echo date_timestamp_get($date);
?>
以上例程的输出类似于:
1272509157
注释
Using U as the parameter to <span class="function">DateTime::format is an alternative when using PHP 5.2.
参见
- DateTime::setTimestamp
- DateTime::format
DateTime::getTimezone
DateTimeImmutable::getTimezone
DateTimeInterface::getTimezone
date_timezone_get
Return time zone relative to given DateTime
说明
面向对象风格
public <span class="type">DateTimeZonefalse DateTime::getTimezone ( <span class="methodparam">void )
public <span class="type">DateTimeZonefalse DateTimeImmutable::getTimezone ( <span class="methodparam">void )
public <span class="type">DateTimeZonefalse DateTimeInterface::getTimezone ( <span class="methodparam">void )
过程化风格
DateTimeZone<span
class="type">false <span
class="methodname">date_timezone_get ( <span
class="methodparam">DateTimeInterface
$object
)
Return time zone relative to given DateTime.
参数
object
仅为过程化风格:由 date_create 返回的
DateTime 类型的对象。
返回值
Returns a DateTimeZone object on success
或者在失败时返回 false
.
范例
示例 #1 DateTime::getTimezone example
面向对象风格
<?php
$date = new DateTime(null, new DateTimeZone('Europe/London'));
$tz = $date->getTimezone();
echo $tz->getName();
?>
过程化风格
<?php
$date = date_create(null, timezone_open('Europe/London'));
$tz = date_timezone_get($date);
echo timezone_name_get($tz);
?>
以上例程会输出:
Europe/London
参见
- DateTime::setTimezone
DateTime::__wakeup
DateTimeImmutable::__wakeup
DateTimeInterface::__wakeup
The __wakeup handler
说明
public void DateTime::__wakeup ( <span class="methodparam">void )
public void DateTimeImmutable::__wakeup ( <span class="methodparam">void )
public void DateTimeInterface::__wakeup ( <span class="methodparam">void )
The __wakeup() handler.
参数
此函数没有参数。
返回值
Initializes a DateTime object.
简介
Representation of time zone.
类摘要
DateTimeZone
class DateTimeZone {
/* 常量 */
const int
DateTimeZone::AFRICA
= 1 ;
const int
DateTimeZone::AMERICA
= 2 ;
const int
DateTimeZone::ANTARCTICA
= 4 ;
const int
DateTimeZone::ARCTIC
= 8 ;
const int
DateTimeZone::ASIA
= 16 ;
const int
DateTimeZone::ATLANTIC
= 32 ;
const int
DateTimeZone::AUSTRALIA
= 64 ;
const int
DateTimeZone::EUROPE
= 128 ;
const int
DateTimeZone::INDIAN
= 256 ;
const int
DateTimeZone::PACIFIC
= 512 ;
const int
DateTimeZone::UTC
= 1024 ;
const int
DateTimeZone::ALL
= 2047 ;
const int
DateTimeZone::ALL_WITH_BC
= 4095 ;
const int
DateTimeZone::PER_COUNTRY
= 4096 ;
/* 方法 */
public <span
class="methodname">__construct ( <span
class="methodparam">string $timezone
)
public array getLocation ( <span class="methodparam">void )
public string getName ( <span class="methodparam">void )
public int <span
class="methodname">getOffset ( <span
class="type">DateTime $datetime
)
public <span
class="type">arrayfalse <span
class="methodname">getTransitions ([ <span
class="methodparam">int $timestampBegin
<span
class="initializer"> = PHP_INT_MIN
[, <span
class="methodparam">int $timestampEnd
<span
class="initializer"> = PHP_INT_MAX
]] )
public <span class="modifier">static array <span class="methodname">listAbbreviations ( <span class="methodparam">void )
public <span
class="modifier">static array <span
class="methodname">listIdentifiers ([ <span
class="methodparam">int $what
<span
class="initializer"> = DateTimeZone::ALL [, <span
class="methodparam">string $country
<span
class="initializer"> = null
]] )
}
预定义常量
DateTimeZone::AFRICA
Africa time zones.
DateTimeZone::AMERICA
America time zones.
DateTimeZone::ANTARCTICA
Antarctica time zones.
DateTimeZone::ARCTIC
Arctic time zones.
DateTimeZone::ASIA
Asia time zones.
DateTimeZone::ATLANTIC
Atlantic time zones.
DateTimeZone::AUSTRALIA
Australia time zones.
DateTimeZone::EUROPE
Europe time zones.
DateTimeZone::INDIAN
Indian time zones.
DateTimeZone::PACIFIC
Pacific time zones.
DateTimeZone::UTC
UTC time zones.
DateTimeZone::ALL
All time zones.
DateTimeZone::ALL_WITH_BC
All time zones including backwards compatible.
DateTimeZone::PER_COUNTRY
Time zones per country.
DateTimeZone::__construct
timezone_open
创建新的DateTimeZone对象
说明
面向对象风格
public <span
class="methodname">DateTimeZone::__construct ( <span
class="methodparam">string $timezone
)
过程化风格
DateTimeZone <span
class="methodname">timezone_open ( <span
class="methodparam">string $timezone
)
创建新的DateTimeZone对象.
参数
timezone
所支持的 timezone names之一.
返回值
成功时返回 DateTimeZone对象.
过程化风格在失败时返回 false
。
错误/异常
如果提供的时区无效,此方法将抛出 <span class="classname">Exception异常.
范例
示例 #1 Catching errors when instantiating <span class="classname">DateTimeZone
<?php
// Error handling by catching exceptions
$timezones = array('Europe/London', 'Mars/Phobos', 'Jupiter/Europa');
foreach ($timezones as $tz) {
try {
$mars = new DateTimeZone($tz);
} catch(Exception $e) {
echo $e->getMessage() . '<br />';
}
}
?>
以上例程会输出:
DateTimeZone::__construct() [datetimezone.--construct]: Unknown or bad timezone (Mars/Phobos)
DateTimeZone::__construct() [datetimezone.--construct]: Unknown or bad timezone (Jupiter/Europa)
DateTimeZone::getLocation
timezone_location_get
返回与时区相关的定位信息。
说明
面向对象风格
public array DateTimeZone::getLocation ( <span class="methodparam">void )
过程化风格
array <span
class="methodname">timezone_location_get ( <span
class="methodparam">DateTimeZone
$object
)
返回与时区相关的定位信息,包括国家代码,纬度/经度和解释。
参数
object
仅过程化风格:由 timezone_open 返回的
DateTimeZone 对象。
返回值
数组,包含与时区相关的定位信息。
范例
示例 #1 DateTimeZone::getLocation 函数的范例:
<?php
$tz = new DateTimeZone("Europe/Prague");
print_r($tz->getLocation());
print_r(timezone_location_get($tz));
?>
以上例程会输出:
Array
(
[country_code] => CZ
[latitude] => 50.08333
[longitude] => 14.43333
[comments] =>
)
Array
(
[country_code] => CZ
[latitude] => 50.08333
[longitude] => 14.43333
[comments] =>
)
DateTimeZone::getName
timezone_name_get
返回时区名称。
说明
面向对象风格
public string DateTimeZone::getName ( <span class="methodparam">void )
过程化风格
string <span
class="methodname">timezone_name_get ( <span
class="methodparam">DateTimeZone
$object
)
返回时区名称。
参数
object
DateTimeZone对象。
返回值
时区名称列表之一。
DateTimeZone::getOffset
timezone_offset_get
返回相对于 GMT 的时差。
说明
面向对象风格
public int <span
class="methodname">DateTimeZone::getOffset ( <span
class="methodparam">DateTime
$datetime
)
过程化风格
int <span
class="methodname">timezone_offset_get ( <span
class="methodparam">DateTimeZone
$object
, <span
class="type">DateTime $datetime
)
该函数返回 datetime
日期 相对于 GMT 的时差。 GMT 时差是通过
DateTimeZone 对象的时区信息计算出来的。
参数
object
仅过程化风格:由 timezone_open 返回的
DateTimeZone 对象。
datetime
用来计算时差的日期对象。
返回值
成功时返回精确到秒的时差, 或者在失败时返回 false
。
范例
示例 #1 DateTimeZone::getOffset 例子
<?php
// Create two timezone objects, one for Taipei (Taiwan) and one for
// Tokyo (Japan)
$dateTimeZoneTaipei = new DateTimeZone("Asia/Taipei");
$dateTimeZoneJapan = new DateTimeZone("Asia/Tokyo");
// Create two DateTime objects that will contain the same Unix timestamp, but
// have different timezones attached to them.
$dateTimeTaipei = new DateTime("now", $dateTimeZoneTaipei);
$dateTimeJapan = new DateTime("now", $dateTimeZoneJapan);
// Calculate the GMT offset for the date/time contained in the $dateTimeTaipei
// object, but using the timezone rules as defined for Tokyo
// ($dateTimeZoneJapan).
$timeOffset = $dateTimeZoneJapan->getOffset($dateTimeTaipei);
// Should show int(32400) (for dates after Sat Sep 8 01:00:00 1951 JST).
var_dump($timeOffset);
?>
DateTimeZone::getTransitions
timezone_transitions_get
Returns all transitions for the timezone
说明
面向对象风格
public <span
class="type">arrayfalse <span
class="methodname">DateTimeZone::getTransitions ([ <span
class="methodparam">int $timestampBegin
<span
class="initializer"> = PHP_INT_MIN
[, <span
class="methodparam">int $timestampEnd
<span
class="initializer"> = PHP_INT_MAX
]] )
过程化风格
array<span
class="type">false <span
class="methodname">timezone_transitions_get ( <span
class="methodparam">DateTimeZone
$object
[, <span
class="type">int $timestampBegin
=
PHP_INT_MIN
[, <span
class="type">int $timestampEnd
=
PHP_INT_MAX
]] )
参数
object
仅过程化风格:由 timezone_open 返回的
DateTimeZone 对象。
timestampBegin
Begin timestamp.
timestampEnd
End timestamp.
返回值
Returns a numerically indexed array of transition arrays on success,
或者在失败时返回 false
.
Key | Type | Description |
---|---|---|
ts | int | Unix timestamp |
time | string | DateTimeInterface::ISO8601 time string |
offset | int | Offset to UTC in seconds |
isdst | bool | Whether daylight saving time is active |
abbr | string | Timezone abbreviation |
范例
示例 #1 A timezone_transitions_get example
<?php
$timezone = new DateTimeZone("Europe/London");
$transitions = $timezone->getTransitions();
print_r(array_slice($transitions, 0, 3));
?>
以上例程的输出类似于:
Array
(
[0] => Array
(
[ts] => -9223372036854775808
[time] => -292277022657-01-27T08:29:52+0000
[offset] => 3600
[isdst] => 1
[abbr] => BST
)
[1] => Array
(
[ts] => -1691964000
[time] => 1916-05-21T02:00:00+0000
[offset] => 3600
[isdst] => 1
[abbr] => BST
)
[2] => Array
(
[ts] => -1680472800
[time] => 1916-10-01T02:00:00+0000
[offset] => 0
[isdst] =>
[abbr] => GMT
)
)
DateTimeZone::listAbbreviations
timezone_abbreviations_list
返回一个包含 dst (夏令时),时差和时区信息的关联数组。
说明
面向对象风格
public <span class="modifier">static array <span class="methodname">DateTimeZone::listAbbreviations ( <span class="methodparam">void )
过程化风格
array <span class="methodname">timezone_abbreviations_list ( <span class="methodparam">void )
返回值
成功,返回数组, 或者在失败时返回 false
。
范例
示例 #1 timezone_abbreviations_list 函数的范例:
<?php
$timezone_abbreviations = DateTimeZone::listAbbreviations();
print_r($timezone_abbreviations["acst"]);
?>
以上例程的输出类似于:
Array
(
[0] => Array
(
[dst] => 1
[offset] => -14400
[timezone_id] => America/Porto_Acre
)
[1] => Array
(
[dst] => 1
[offset] => -14400
[timezone_id] => America/Eirunepe
)
[2] => Array
(
[dst] => 1
[offset] => -14400
[timezone_id] => America/Rio_Branco
)
[3] => Array
(
[dst] => 1
[offset] => -14400
[timezone_id] => Brazil/Acre
)
)
参见
- timezone_identifiers_list
DateTimeZone::listIdentifiers
timezone_identifiers_list
返回一个包含了所有时区标示符的索引数组。
说明
面向对象风格
public <span
class="modifier">static array <span
class="methodname">DateTimeZone::listIdentifiers ([ <span
class="methodparam">int $what
<span
class="initializer"> = DateTimeZone::ALL [, <span
class="methodparam">string $country
<span
class="initializer"> = null
]] )
过程化风格
array <span
class="methodname">timezone_identifiers_list ([ <span
class="methodparam">int $what
<span
class="initializer"> = DateTimeZone::ALL [, <span
class="methodparam">string $country
<span
class="initializer"> = null
]] )
参数
what
DateTimeZone 类中的常量之一。
country
由两个字母组成,ISO 3166-1 兼容的国家代码。
Note: 只有当
what
被设置为DateTimeZone::PER_COUNTRY
时,该选项才会被使用。
返回值
成功,返回数组,失败则返回false
.
更新日志
版本 | 说明 |
---|---|
5.3.0 | 添加可选的 what 和 country 参数。 |
范例
示例 #1 timezone_identifiers_list 函数的范例:
<?php
$timezone_identifiers = DateTimeZone::listIdentifiers();
for ($i=0; $i < 5; $i++) {
echo "$timezone_identifiers[$i]\n";
}
?>
以上例程的输出类似于:
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
参见
- timezone_abbreviations_list
简介
表示一个时间周期的类。
一个时间周期表示固定量的时间(多少年,月,天,小时等), 也可以表示一个字符串格式的相对时间, 当表示相对时间的时候,字符串格式是 DateTime 类的构造函数所支持的格式。
类摘要
DateInterval
class DateInterval {
/* 属性 */
public integer
$y
;
public integer
$m
;
public integer
$d
;
public integer
$h
;
public integer
$i
;
public integer
$s
;
public float
$f
;
public integer
$invert
;
public mixed
$days
;
/* 方法 */
public <span
class="methodname">__construct ( <span
class="methodparam">string $duration
)
public <span
class="modifier">static <span
class="type">DateIntervalfalse
createFromDateString ( <span
class="methodparam">string $datetime
)
public string
format ( <span
class="type">string $format
)
}
属性
y
多少年。
m
多少月。
d
多少天。
h
多少小时。
i
多少分钟。
s
多少秒。
f
多少微秒。
invert
1 表示一个负的时间周期, 0 表示一个正的时间周期。 请参见: <span
class="methodname">DateInterval::format.
days
如果 DateInterval 对象是由 DateTime::diff
函数创建的, 那么它表示开始日期和结束日期之间包含了多少天。 否则,days
属性为 false
。
在 PHP 5.4.20/5.5.4 之前版本中,此属性不会为 false
, 而是 -99999。
更新日志
版本 | 说明 |
---|---|
7.1.0 | 增加 f 属性。 |
DateInterval::__construct
Creates a new DateInterval object
说明
public <span
class="methodname">DateInterval::__construct ( <span
class="methodparam">string $duration
)
Creates a new DateInterval object.
参数
duration
An interval specification.
The format starts with the letter P, for “period.” Each duration period is represented by an integer value followed by a period designator. If the duration contains time elements, that portion of the specification is preceded by the letter T.
Period Designator | Description |
---|---|
Y | years |
M | months |
D | days |
W | weeks. These get converted into days, so can not be combined with D. |
H | hours |
M | minutes |
S | seconds |
Here are some simple examples. Two days is P2D. Two seconds is PT2S. Six years and five minutes is P6YT5M.
Note:
The unit types must be entered from the largest scale unit on the left to the smallest scale unit on the right. So years before months, months before days, days before minutes, etc. Thus one year and four days must be represented as P1Y4D, not P4D1Y.
The specification can also be represented as a date time. A sample of one year and four days would be P0001-00-04T00:00:00. But the values in this format can not exceed a given period's roll-over-point (e.g. 25 hours is invalid).
These formats are based on the » ISO 8601 duration specification.
错误/异常
Throws an Exception when the duration
cannot be parsed as an interval.
范例
示例 #1 DateInterval example
<?php
$interval = new DateInterval('P2Y4DT6H8M');
var_dump($interval);
?>
以上例程会输出:
object(DateInterval)#1 (8) {
["y"]=>
int(2)
["m"]=>
int(0)
["d"]=>
int(4)
["h"]=>
int(6)
["i"]=>
int(8)
["s"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
bool(false)
}
参见
- DateInterval::format
- DateTime::add
- DateTime::sub
- DateTime::diff
DateInterval::createFromDateString
Sets up a DateInterval from the relative parts of the string
说明
public <span
class="modifier">static <span
class="type">DateIntervalfalse
DateInterval::createFromDateString (
string
$datetime
)
Uses the normal date parsers and sets up a DateInterval from the relative parts of the parsed string.
参数
datetime
A date with relative parts. Specifically, the
relative formats
supported by the parser used for strtotime
and DateTime will be used to construct
the DateInterval.
范例
示例 #1 Parsing valid date intervals
<?php
// Each set of intervals is equal.
$i = new DateInterval('P1D');
$i = DateInterval::createFromDateString('1 day');
$i = new DateInterval('P2W');
$i = DateInterval::createFromDateString('2 weeks');
$i = new DateInterval('P3M');
$i = DateInterval::createFromDateString('3 months');
$i = new DateInterval('P4Y');
$i = DateInterval::createFromDateString('4 years');
$i = new DateInterval('P1Y1D');
$i = DateInterval::createFromDateString('1 year + 1 day');
$i = new DateInterval('P1DT12H');
$i = DateInterval::createFromDateString('1 day + 12 hours');
$i = new DateInterval('PT3600S');
$i = DateInterval::createFromDateString('3600 seconds');
?>
返回值
Returns a new DateInterval instance on
success, 或者在失败时返回 false
.
DateInterval::format
Formats the interval
说明
public string
DateInterval::format ( <span
class="methodparam">string $format
)
Formats the interval.
参数
format
| format
character | Description | Example values |
|--------------------|---------------------------------------------------------------------------------------------------------------|------------------------------|
| % | Literal % | % |
| Y | Years, numeric, at least 2 digits with leading 0 | 01, 03 |
| y | Years, numeric | 1, 3 |
| M | Months, numeric, at least 2 digits with leading 0 | 01, 03, 12 |
| m | Months, numeric | 1, 3, 12 |
| D | Days, numeric, at least 2 digits with leading 0 | 01, 03, 31 |
| d | Days, numeric | 1, 3, 31 |
| a | Total number of days as a result of a DateTime::diff or (unknown) otherwise | 4, 18, 8123 |
| H | Hours, numeric, at least 2 digits with leading 0 | 01, 03, 23 |
| h | Hours, numeric | 1, 3, 23 |
| I | Minutes, numeric, at least 2 digits with leading 0 | 01, 03, 59 |
| i | Minutes, numeric | 1, 3, 59 |
| S | Seconds, numeric, at least 2 digits with leading 0 | 01, 03, 57 |
| s | Seconds, numeric | 1, 3, 57 |
| F | Microseconds, numeric, at least 6 digits with leading 0 | 007701, 052738, 428291 |
| f | Microseconds, numeric | 7701, 52738, 428291 |
| R | Sign "-" when negative, "+" when positive | -, + |
| r | Sign "-" when negative, empty when positive | -, |
返回值
Returns the formatted interval.
注释
Note:
The DateInterval::format method does not recalculate carry over points in time strings nor in date segments. This is expected because it is not possible to overflow values like "32 days" which could be interpreted as anything from "1 month and 4 days" to "1 month and 1 day".
更新日志
版本 | 说明 |
---|---|
7.1.0 | The F and f format characters were added. |
范例
示例 #1 DateInterval example
<?php
$interval = new DateInterval('P2Y4DT6H8M');
echo $interval->format('%d days');
?>
以上例程会输出:
4 days
示例 #2 DateInterval and carry over points
<?php
$interval = new DateInterval('P32D');
echo $interval->format('%d days');
?>
以上例程会输出:
32 days
示例 #3 DateInterval and <span class="methodname">DateTime::diff with the %a and %d modifiers
<?php
$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);
// %a will output the total number of days.
echo $interval->format('%a total days')."\n";
// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');
?>
以上例程会输出:
31 total days
1 month, 0 days
参见
- DateTime::diff
简介
DatePeriod 类表示一个时间周期。
一个时间周期可以用来在给定的一段时间之内, 以一定的时间间隔进行迭代。
类摘要
DatePeriod
class DatePeriod <span class="oointerface">implements <span class="interfacename">Traversable {
/* 常量 */
const integer
DatePeriod::EXCLUDE_START_DATE
= 1 ;
/* 属性 */
public integer
$recurrences
;
public boolean
$include_start_date
;
public <span
class="type">DateTimeInterface $start
;
public <span
class="type">DateTimeInterface $current
;
public <span
class="type">DateTimeInterface $end
;
public <span
class="type">DateInterval $interval
;
/* 方法 */
public <span
class="methodname">__construct ( <span
class="methodparam">DateTimeInterface
$start
, <span
class="type">DateInterval $interval
, <span
class="methodparam">int $recurrences
[, int
$options
] )
public <span
class="methodname">__construct ( <span
class="methodparam">DateTimeInterface
$start
, <span
class="type">DateInterval $interval
, <span
class="methodparam">DateTimeInterface
$end
[, <span
class="type">int $options
] )
public <span
class="methodname">__construct ( <span
class="methodparam">string $isostr
[, int
$options
] )
public <span class="type">DateInterval <span class="methodname">getDateInterval ( <span class="methodparam">void )
public <span class="type">DateTimeInterface<span class="type">null <span class="methodname">getEndDate ( <span class="methodparam">void )
public <span class="type">intnull <span class="methodname">getRecurrences ( <span class="methodparam">void )
public <span class="type">DateTimeInterface <span class="methodname">getStartDate ( <span class="methodparam">void )
}
预定义常量
DatePeriod::EXCLUDE_START_DATE
在 DatePeriod::__construct
构造函数中使用,表示不包含开始时间。
属性
recurrences
如果通过显式的传入 $recurrences 来创建的 <span
class="classname">DatePeriod 实例, 那么这个参数表示循环次数。
参见: DatePeriod::getRecurrences。
include_start_date
在循环过程中,是否包含开始时间。
start
时间周期的开始时间。
current
表示在时间周期内迭代的时候,当前的时间。
end
时间周期的结束时间。
interval
ISO 8601 格式的间隔。
更新日志
版本 | 说明 |
---|---|
5.3.27, 5.4.17 | 公开以下属性:recurrences , include_start_date ,start , current ,end 和 interval 。 |
DatePeriod::__construct
Creates a new DatePeriod object
说明
public <span
class="methodname">DatePeriod::__construct ( <span
class="methodparam">DateTimeInterface
$start
, <span
class="type">DateInterval $interval
, <span
class="methodparam">int $recurrences
[, int
$options
] )
public <span
class="methodname">DatePeriod::__construct ( <span
class="methodparam">DateTimeInterface
$start
, <span
class="type">DateInterval $interval
, <span
class="methodparam">DateTimeInterface
$end
[, <span
class="type">int $options
] )
public <span
class="methodname">DatePeriod::__construct ( <span
class="methodparam">string $isostr
[, int
$options
] )
Creates a new DatePeriod object.
参数
start
The start date of the period.
interval
The interval between recurrences within the period.
recurrences
The number of recurrences.
end
The end date of the period.
isostr
An ISO 8601 repeating interval specification.
options
Can be set to DatePeriod::EXCLUDE_START_DATE
to exclude the start
date from the set of recurring dates within the period.
更新日志
版本 | 说明 |
---|---|
5.5.8 | end type changed to DateTimeInterface. Previously, DateTime. |
5.5.0 | start type changed to DateTimeInterface. Previously, DateTime. |
范例
示例 #1 DatePeriod example
<?php
$start = new DateTime('2012-07-01');
$interval = new DateInterval('P7D');
$end = new DateTime('2012-07-31');
$recurrences = 4;
$iso = 'R4/2012-07-01T00:00:00Z/P7D';
// All of these periods are equivalent.
$period = new DatePeriod($start, $interval, $recurrences);
$period = new DatePeriod($start, $interval, $end);
$period = new DatePeriod($iso);
// By iterating over the DatePeriod object, all of the
// recurring dates within that period are printed.
foreach ($period as $date) {
echo $date->format('Y-m-d')."\n";
}
?>
以上例程会输出:
2012-07-01
2012-07-08
2012-07-15
2012-07-22
2012-07-29
示例 #2 DatePeriod example with
DatePeriod::EXCLUDE_START_DATE
****
<?php
$start = new DateTime('2012-07-01');
$interval = new DateInterval('P7D');
$end = new DateTime('2012-07-31');
$period = new DatePeriod($start, $interval, $end,
DatePeriod::EXCLUDE_START_DATE);
// By iterating over the DatePeriod object, all of the
// recurring dates within that period are printed.
// Note that, in this case, 2012-07-01 is not printed.
foreach ($period as $date) {
echo $date->format('Y-m-d')."\n";
}
?>
以上例程会输出:
2012-07-08
2012-07-15
2012-07-22
2012-07-29
注释
Unbound numbers of repetitions as specified by ISO 8601 section 4.5
"Recurring time interval" are not supported, i.e. neither passing
"R/..." as isostr
nor passing null
as end
would work.
DatePeriod::getDateInterval
Gets the interval
说明
面向对象风格
public <span class="type">DateInterval <span class="methodname">DatePeriod::getDateInterval ( <span class="methodparam">void )
Gets a DateInterval <span class="type">object representing the interval used for the period.
参数
此函数没有参数。
返回值
Returns a DateInterval <span class="type">object
范例
示例 #1 DatePeriod::getDateInterval example
<?php
$period = new DatePeriod('R7/2016-05-16T00:00:00Z/P1D');
$interval = $period->getDateInterval();
echo $interval->format('%d day');
?>
以上例程会输出:
1 day
参见
- DatePeriod::getStartDate
- DatePeriod::getEndDate
DatePeriod::getEndDate
Gets the end date
说明
面向对象风格
public <span class="type">DateTimeInterface<span class="type">null <span class="methodname">DatePeriod::getEndDate ( <span class="methodparam">void )
Gets the end date of the period.
参数
此函数没有参数。
返回值
Returns null
if the DatePeriod does
not have an end date. For example, when initialized with the
recurrences
parameter, or the isostr
parameter without an end date.
Returns a DateTimeImmutable <span
class="type">object when the <span
class="classname">DatePeriod is initialized with a <span
class="classname">DateTimeImmutable <span
class="type">object as the end
parameter.
Returns a DateTime <span class="type">object otherwise.
范例
示例 #1 DatePeriod::getEndDate example
<?php
$period = new DatePeriod(
new DateTime('2016-05-16T00:00:00Z'),
new DateInterval('P1D'),
new DateTime('2016-05-20T00:00:00Z')
);
$start = $period->getEndDate();
echo $start->format(DateTime::ISO8601);
?>
以上例程会输出:
2016-05-20T00:00:00+0000
示例 #2 DatePeriod::getEndDate without an end date
<?php
$period = new DatePeriod(
new DateTime('2016-05-16T00:00:00Z'),
new DateInterval('P1D'),
7
);
var_dump($period->getEndDate());
?>
以上例程会输出:
NULL
参见
- DatePeriod::getStartDate
- DatePeriod::getDateInterval
DatePeriod::getRecurrences
Gets the number of recurrences
说明
面向对象风格
public <span class="type">intnull <span class="methodname">DatePeriod::getRecurrences ( <span class="methodparam">void )
Get the number of recurrences.
参数
此函数没有参数。
返回值
Returns the number of recurrences.
参见
DatePeriod::getStartDate
Gets the start date
说明
面向对象风格
public <span class="type">DateTimeInterface <span class="methodname">DatePeriod::getStartDate ( <span class="methodparam">void )
Gets the start date of the period.
参数
此函数没有参数。
返回值
Returns a DateTimeImmutable <span
class="type">object when the <span
class="classname">DatePeriod is initialized with a <span
class="classname">DateTimeImmutable <span
class="type">object as the start
parameter.
Returns a DateTime <span class="type">object otherwise.
范例
示例 #1 DatePeriod::getStartDate example
<?php
$period = new DatePeriod('R7/2016-05-16T00:00:00Z/P1D');
$start = $period->getStartDate();
echo $start->format(DateTime::ISO8601);
?>
以上例程会输出:
2016-05-16T00:00:00+0000
参见
- DatePeriod::getEndDate
- DatePeriod::getDateInterval