php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77885 DateTime function is taking timezone in consideration and always considering se
Submitted: 2019-04-12 20:01 UTC Modified: 2019-04-12 20:40 UTC
From: vinodh dot basavani at darwinbox dot io Assigned:
Status: Not a bug Package: PHP options/info functions
PHP Version: 7.1.28 OS: Debian GNU/Linux 9.8 (stretch)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: vinodh dot basavani at darwinbox dot io
New email:
PHP Version: OS:

 

 [2019-04-12 20:01 UTC] vinodh dot basavani at darwinbox dot io
Description:
------------
<?php


$dt = new DateTime("1970-01-01 ".date("H:i:s"));
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('r');
echo "\n";
$seconds = (int)$dt->getTimestamp();
echo date("H:i:s",$seconds);
echo "\n";

?>

even if i am setting the timezone to UTC, it is taking only the server time.

Expected result:
----------------
It should take the UTC time zone.

Actual result:
--------------
It should take the UTC time zone.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-04-12 20:24 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-04-12 20:24 UTC] requinix@php.net
That is not how timezones and Unix timestamps work.

You created a date and time according to the server timezone. Changing the timezone from there only affects the string representation of it - the timestamp is the same. Getting that timestamp and formatting it according to the server timezone will get you right back to where you started.
 [2019-04-12 20:30 UTC] vinodh dot basavani at darwinbox dot io
$dt = new DateTime("1970-01-01 ".date("H:i:s"), new DateTimeZone('UTC'));
        $seconds = (int)$dt->getTimestamp();
        echo date("H:i:s",$seconds);

This snippet works fine in earlier versions of php i.e it is not taking the server time zone. but when i updated to the specified version.
it is always taking the server time and ignoring the timezone specified.
 [2019-04-12 20:32 UTC] requinix@php.net
You know the default timezone in PHP is UTC, right?
 [2019-04-12 20:33 UTC] vinodh dot basavani at darwinbox dot io
No..its in IST time zone.
 [2019-04-12 20:34 UTC] requinix@php.net
What does date_default_timezone_get() return?
 [2019-04-12 20:39 UTC] vinodh dot basavani at darwinbox dot io
<?php

echo date_default_timezone_get();
?>


Asia/Calcutta
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jan 02 13:01:30 2025 UTC