函数名称:DateInterval::__construct()
适用版本:PHP 5 >= 5.3.0, PHP 7
函数描述:DateInterval::__construct() 函数用于创建一个新的 DateInterval 对象。
用法:
DateInterval::__construct ( string $interval_spec )
参数:
返回值:无返回值。
示例:
$interval = new DateInterval('P2Y3M5DT12H30M3S');
var_dump($interval);
// 输出:
/*
object(DateInterval)#1 (16) {
["y"]=>
int(2)
["m"]=>
int(3)
["d"]=>
int(5)
["h"]=>
int(12)
["i"]=>
int(30)
["s"]=>
int(3)
["weekday"]=>
int(0)
["weekday_behavior"]=>
int(0)
["first_last_day_of"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
bool(false)
["special_type"]=>
int(0)
["special_amount"]=>
int(0)
["have_weekday_relative"]=>
int(0)
["have_special_relative"]=>
int(0)
["__construct"]=>
string(39) "DateInterval::__construct(P2Y3M5DT12H30M3S)"
}
*/
在上面的示例中,我们创建了一个名为 $interval 的 DateInterval 对象,其中间隔字符串为 P2Y3M5DT12H30M3S。通过 var_dump() 函数可以查看到该对象的属性值。可以看出,该对象表示了一个持续时间为 2 年 3 个月 5 天 12 小时 30 分钟 3 秒的间隔对象。