文字

DateInterval::createFromDateString

(PHP 5 >= 5.3.0, PHP 7)

DateInterval::createFromDateStringSets up a DateInterval from the relative parts of the string

说明

public static DateInterval DateInterval::createFromDateString ( string $time )

Uses the normal date parsers and sets up a DateInterval from the relative parts of the parsed string.

参数

time

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.

范例

Example #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.

用户评论:

[#1] msleman at boot dot dot dot com [2010-10-28 20:39:03]

Sample usage:

<?php
$interval 
DateInterval::createFromDateString('1 month');
?>

[#2] Anonymous [2009-09-28 12:02:42]

DateInterval::createFromDateString ( string $time )

When the manual says "Uses the normal date parsers" it means that this function cannot take $time = ISO8601 strings like "P7D".  If you want to use those, you must use the constructor.

上一篇: 下一篇: