php / php/php-src

Unexpected Results with PHP8 date_diff for Different Timezones

Open
#13,857 3 comments 1 reaction 1 assignee View on GitHub

@derickr is already working on this.

Since Apr 1, 2024.

Bug Extension: date Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

In PHP 8.1.25, running date_diff against different time zones returned unexpected results.

https://3v4l.org/RFQMt

The following code:

<?php
date_default_timezone_set('Asia/Tokyo');
$nowDate = (new DateTime('2024-04-01 00:49:22'))->setTimeZone(new DateTimeZone('Asia/Tokyo'));
$endDate = (new DateTime('2024-04-01 08:48:22'))->setTimeZone(new DateTimeZone('UTC'));
echo date_diff($nowDate, $endDate)->format('%Y-%M-%D %H:%I:%S');

Resulted in this output:

-1-11-31 07:59:00

But I expected this output instead:

00-00-00 07:59:00
PHP Version

PHP 8.1.25

Operating System

Ubuntu 22.04

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.