牛牛在线工具

DatePeriod::getStartDate()

收录 8868 个 PHP 函数,含用途说明与用法示例。

DatePeriod::getStartDate()

获取 DatePeriod 对象的起始日期

PHP函数:DatePeriod::getStartDate() 适用版本:PHP 5 >= 5.6.5, PHP 7 用法:DatePeriod::getStartDate() 方法用于获取 DatePeriod 对象的起始日期。 语法:public DateTimeInterface|null DatePeriod::getStartDate (void) 参数:此方法不接受任何参数。 返回值:如果 DatePeriod 对象的起始日期已设置,则返回 DateTimeInterface 对象;如果未设置,则返回 null。 示例: // 创建一个 DatePeriod 对象 $start = new DateTime('2022-01-01'); $end = new DateTime('2022-01-10'); $interval = new DateInterval('P1D'); $period = new DatePeriod($start, $interval, $end); // 获取 DatePeriod 对象的起始日期 $startDate = $period->getStartDate(); if ($startDate instanceof DateTimeInterface) { echo "起始日期: " . $startDate->format('Y-m-d'); } else { echo "起始日期未设置"; } 输出: 起始日期: 2022-01-01 在这个示例中,我们首先创建了一个 DatePeriod 对象,该对象表示从 2022-01-01 到 2022-01-10 的日期范围,间隔为 1 天。然后,我们使用 getStartDate() 方法获取 DatePeriod 对象的起始日期,并通过判断返回值是否为 DateTimeInterface 对象来确定起始日期是否已设置。最后,我们使用 format() 方法将起始日期格式化为年-月-日的形式,并将其输出到屏幕上。