PHP Conference Nagoya 2025

IntlCalendar::fromDateTime

(PHP 5 >= 5.5.0, PHP 7, PHP 8, PECL >= 3.0.0a2)

IntlCalendar::fromDateTimeCreate an IntlCalendar from a DateTime object or string

说明

面向对象风格

public static IntlCalendar::fromDateTime(DateTime|string $datetime, ?string $locale = null): ?IntlCalendar

过程化风格

intlcal_from_date_time(DateTime|string $datetime, ?string $locale = null): ?IntlCalendar

Creates an IntlCalendar object either from a DateTime object or from a string from which a DateTime object can be built.

The new calendar will represent not only the same instant as the given DateTime (subject to precision loss for dates very far into the past or future), but also the same timezone (subject to the caveat that different timezone databases will be used, and therefore the results may differ).

参数

datetime

A DateTime object or a string that can be passed to DateTime::__construct().

返回值

The created IntlCalendar object or null in case of failure. If a string is passed, any exception that occurs inside the DateTime constructor is propagated.

示例

示例 #1 IntlCalendar::fromDateTime()

<?php
ini_set
('date.timezone', 'Europe/Lisbon');

//same as IntlCalendar::fromDateTime(new DateTime(...))
$cal1 = IntlCalendar::fromDateTime('2013-02-28 00:01:02 Europe/Berlin');

//Note the timezone is Europe/Berlin, not the default Europe/Lisbon
echo IntlDateFormatter::formatObject($cal1, 'yyyy MMMM d HH:mm:ss VVVV', 'de_DE'), "\n";

以上示例会输出:

2013 Februar 28 00:01:02 Deutschland Zeit

添加备注

用户贡献的备注 1 note

up
2
SenseException
10 years ago
Don't forget that fromDateTime() doesn't set any locale and that the default one will be set. In my case it was en_US_POSIX.

If you wan't to create a IntlCalendar object containing the values of a DateTime object with your locale, use createInstance() instead and do a

<?php
$intlCalendar
->setTime($dateTime->getTimestamp() * 1000);
?>

IntlCalendar works with milliseconds so you need to multiply the timestamp with 1000.

Using fromDateTime() can cause unwanted behaviour like getFirstDayOfWeek() returning a wrong integer.
To Top