Методы определения летнего времени в PHP с примерами кода

Чтобы определить летнее время в PHP, вы можете использовать различные методы. Вот несколько примеров:

Метод 1: использование date()и gmdate()

// Get the current timestamp
$currentTimestamp = time();
// Get the current time in UTC
$utcTime = gmdate('Y-m-d H:i:s', $currentTimestamp);
// Get the current time in the local timezone
$localTime = date('Y-m-d H:i:s', $currentTimestamp);
// Check if the local time and UTC time are different
$isDaylightSavingTime = $localTime !== $utcTime;
if ($isDaylightSavingTime) {
    // Daylight saving time is in effect
    // Add your code here
} else {
    // Daylight saving time is not in effect
    // Add your code here
}

Метод 2: использование DateTimeи DateTimeZone

// Create DateTime objects for the current time in UTC and local timezone
$utcDateTime = new DateTime('now', new DateTimeZone('UTC'));
$localDateTime = new DateTime('now', new DateTimeZone(date_default_timezone_get()));
// Get the timezone offset in seconds
$timezoneOffset = $localDateTime->getOffset() - $utcDateTime->getOffset();
// Check if the timezone offset is different from zero
$isDaylightSavingTime = $timezoneOffset !== 0;
if ($isDaylightSavingTime) {
    // Daylight saving time is in effect
    // Add your code here
} else {
    // Daylight saving time is not in effect
    // Add your code here
}

Метод 3: использование IntlCalendar

// Create an IntlCalendar object for the current time in the local timezone
$calendar = IntlCalendar::fromDateTime('now');
// Get the DST offset in milliseconds
$dstOffset = $calendar->get(IntlCalendar::DST_OFFSET);
// Check if the DST offset is different from zero
$isDaylightSavingTime = $dstOffset !== 0;
if ($isDaylightSavingTime) {
    // Daylight saving time is in effect
    // Add your code here
} else {
    // Daylight saving time is not in effect
    // Add your code here
}