Authentication Services
Command Line Specific Extensions
Compression and Archive Extensions
Cryptography Extensions
Database Extensions
Date and Time Related Extensions
File System Related Extensions
Human Language and Character Encoding Support
Image Processing and Generation
Mail Related Extensions
Mathematical Extensions
Non-Text MIME Output
Process Control Extensions
Other Basic Extensions
Other Services
Search Engine Extensions
Server Specific Extensions
Session Extensions
Text Processing
Variable and Type Related Extensions
Web Services
Windows Only Extensions
XML Manipulation
GUI Extensions
Keyboard Shortcuts
?
This help
Next menu item
Previous menu item
Previous man page
Next man page
Scroll to bottom
Scroll to top
Goto homepage
Goto search
(current page)
Focus search box
<?php
$dt
= new
DateTimeImmutable
(
"2015-11-01 00:00:00"
, new
DateTimeZone
(
"America/New_York"
));
echo
"Start: "
,
$dt
->
format
(
"Y-m-d H:i:s P"
),
PHP_EOL
;
$dt
=
$dt
->
add
(new
DateInterval
(
"PT3H"
));
echo
"End: "
,
$dt
->
format
(
"Y-m-d H:i:s P"
),
PHP_EOL
;
?>
Start: 2015-11-01 00:00:00 -04:00
End: 2015-11-01 02:00:00 -05:00
示例 #2 DateTimeImmutable::modify 和 strtotime 递增/减单个组件值
在 DST 转换上添加 +24 小时将准确添加 24 小时,如日期/时间字符串中所示(除非开始/结束时间位于转换点)。
<?php
$dt
= new
DateTimeImmutable
(
"2015-11-01 00:00:00"
, new
DateTimeZone
(
"America/New_York"
));
echo
"Start: "
,
$dt
->
format
(
"Y-m-d H:i:s P"
),
PHP_EOL
;
$dt
=
$dt
->
modify
(
"+24 hours"
);
echo
"End: "
,
$dt
->
format
(
"Y-m-d H:i:s P"
),
PHP_EOL
;
?>
Start: 2015-11-01 00:00:00 -04:00
End: 2015-11-02 00:00:00 -05:00
<?php
echo
"Normal year:\n"
;
// 2 月有 28 天
$dt
= new
DateTimeImmutable
(
"2015-01-31 00:00:00"
, new
DateTimeZone
(
"America/New_York"
));
echo
"Start: "
,
$dt
->
format
(
"Y-m-d H:i:s P"
),
PHP_EOL
;
$dt
=
$dt
->
modify
(
"+1 month"
);
echo
"End: "
,
$dt
->
format
(
"Y-m-d H:i:s P"
),
PHP_EOL
;
echo
"Leap year:\n"
;
// 2 月有 29 天
$dt
= new
DateTimeImmutable
(
"2016-01-31 00:00:00"
, new
DateTimeZone
(
"America/New_York"
));
echo
"Start: "
,
$dt
->
format
(
"Y-m-d H:i:s P"
),
PHP_EOL
;
$dt
=
$dt
->
modify
(
"+1 month"
);
echo
"End: "
,
$dt
->
format
(
"Y-m-d H:i:s P"
),
PHP_EOL
;
?>
Normal year:
Start: 2015-01-31 00:00:00 -05:00
End: 2015-03-03 00:00:00 -05:00
Leap year:
Start: 2016-01-31 00:00:00 -05:00
End: 2016-03-02 00:00:00 -05:00
要获取下个月的最后一天(例如防止溢出),可以使用
last day of
格式。
<?php
echo
"Normal year:\n"
;
// 2 月有 28 天
$dt
= new
DateTimeImmutable
(
"2015-01-31 00:00:00"
, new
DateTimeZone
(
"America/New_York"
));
echo
"Start: "
,
$dt
->
format
(
"Y-m-d H:i:s P"
),
PHP_EOL
;
$dt
=
$dt
->
modify
(
"last day of next month"
);
echo
"End: "
,
$dt
->
format
(
"Y-m-d H:i:s P"
),
PHP_EOL
;
echo
"Leap year:\n"
;
// 2 月有 29 天
$dt
= new
DateTimeImmutable
(
"2016-01-31 00:00:00"
, new
DateTimeZone
(
"America/New_York"
));
echo
"Start: "
,
$dt
->
format
(
"Y-m-d H:i:s P"
),
PHP_EOL
;
$dt
=
$dt
->
modify
(
"last day of next month"
);
echo
"End: "
,
$dt
->
format
(
"Y-m-d H:i:s P"
),
PHP_EOL
;
?>
Normal year:
Start: 2015-01-31 00:00:00 -05:00
End: 2015-02-28 00:00:00 -05:00
Leap year:
Start: 2016-01-31 00:00:00 -05:00
End: 2016-02-29 00:00:00 -05:00