Skip to content

date() <-> strtotime() conversion bug during DST switch.  #16686

@atreshilov

Description

@atreshilov

Description

This happens in my application one hour per year. During DST switch only. I noticed this bug in PHP7 years ago under CentOS, but no fix for years, so I reported it.

$ date
Sun Nov 3 01:57:02 CST 2024

The following code:

<?php

date_default_timezone_set('America/Chicago');

$ts=time();
var_dump($ts);
$sql_format=date("Y-m-d H:i:s", $ts);
var_dump($sql_format);

$ts_back=strtotime($sql_format);
var_dump($ts_back);
$sql_format_back=date("Y-m-d H:i:s", $ts_back);
var_dump($sql_format_back);

Resulted in this output:

int(1730620622)
string(19) "2024-11-03 01:57:02"
int(1730617022)                                           <-- DIFFERENCE
string(19) "2024-11-03 01:57:02"

But I expected this output instead:

int(1730620622)
string(19) "2024-11-03 01:57:02"
int(1730620622)
string(19) "2024-11-03 01:57:02"

The difference between (int) values is 3600 seconds in the actual result.

Once the hour is switched to 2AM everything works as it should:

$ date
Sun Nov 3 02:00:19 CST 2024
$ php dt_test.php
int(1730620819)
string(19) "2024-11-03 02:00:19"
int(1730620819)
string(19) "2024-11-03 02:00:19"

PHP Version

PHP 8.1.28

Operating System

Oracle Linux Server release 8.9

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions