牛牛在线工具

IntlGregorianCalendar::createFromDateTime()

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

IntlGregorianCalendar::createFromDateTime()

创建一个 IntlGregorianCalendar 对象,该对象基于给定的 DateTime 对象

函数名:IntlGregorianCalendar::createFromDateTime() 适用版本:PHP 5 >= 5.5.0, PHP 7, PHP 8 用法:IntlGregorianCalendar::createFromDateTime() 函数用于创建一个 IntlGregorianCalendar 对象,该对象基于给定的 DateTime 对象。 语法:public static IntlGregorianCalendar IntlGregorianCalendar::createFromDateTime(DateTimeInterface $dateTime) 参数: $dateTime: 必需。一个实现了 DateTimeInterface 接口的 DateTime 对象。 返回值:返回一个 IntlGregorianCalendar 对象,或者在失败时返回 FALSE。 示例: // 创建一个 DateTime 对象 $dateTime = new DateTime("2022-01-01 12:00:00", new DateTimeZone("Asia/Shanghai")); // 使用 IntlGregorianCalendar::createFromDateTime() 创建 IntlGregorianCalendar 对象 $calendar = IntlGregorianCalendar::createFromDateTime($dateTime); // 检查对象是否创建成功 if ($calendar !== false) { // 输出 IntlGregorianCalendar 对象的属性 echo "Calendar Type: " . $calendar->getType() . "<br>"; echo "First Day of Week: " . $calendar->getFirstDayOfWeek() . "<br>"; echo "Minimal Days in First Week: " . $calendar->getMinimalDaysInFirstWeek() . "<br>"; } else { echo "Failed to create IntlGregorianCalendar object."; } 输出结果: Calendar Type: gregorian First Day of Week: 1 Minimal Days in First Week: 1 以上示例创建了一个 DateTime 对象,并使用 IntlGregorianCalendar::createFromDateTime() 函数基于该 DateTime 对象创建了一个 IntlGregorianCalendar 对象。然后,我们通过调用 IntlGregorianCalendar 对象的属性方法来获取相关信息,如日历类型、一周的第一天以及第一周的最少天数。如果创建对象失败,则输出相应的错误信息。