php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #81319 Unfortunate example on time() man page
Submitted: 2021-08-01 14:41 UTC Modified: 2021-08-04 13:39 UTC
From: will dot duncan dot nn at gmail dot com Assigned:
Status: Verified Package: Date/time related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2021-08-01 14:41 UTC] will dot duncan dot nn at gmail dot com
Description:
------------
https://www.php.net/manual/en/function.time.php#118876

The use is implying that UTC is affected by daylight saving, which is obviously not true https://en.wikipedia.org/wiki/Coordinated_Universal_Time

The user is implying that "not every day has 24 hours," however, for UTC, which is used for the `time()` function, this is never the case. Under "Mechanism" on the Wikipedia article, you can easily find the following quote: "Each day contains 24 hours and each hour contains 60 minutes."

I am reporting this as this is the #2 most upvoted note for this page. It is incredibly misleading and inaccurate. Notes like this one can greatly damage new developers browsing the documentation, especially when said note appears on the top.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-08-01 14:42 UTC] will dot duncan dot nn at gmail dot com
Description:
------------
https://www.php.net/manual/en/function.time.php#118876

The user is implying that UTC is affected by daylight saving, which is obviously not true https://en.wikipedia.org/wiki/Coordinated_Universal_Time

The user is implying that "not every day has 24 hours," however, for UTC, which is used for the `time()` function, this is never the case. Under "Mechanism" on the Wikipedia article, you can easily find the following quote: "Each day contains 24 hours and each hour contains 60 minutes."

I am reporting this as this is the #2 most upvoted note for this page. It is incredibly misleading and inaccurate. Notes like this one can greatly damage new developers browsing the documentation, especially when said note appears on the top.
 [2021-08-02 12:59 UTC] antonino dot spampinato86 at gmail dot com
It means if your local time is different from UTC (UTC+00:00/GMT+0000) you have to adjust the time zone to see the date in UTC, directly with the date function and without changing the timezone. Without using native php functions, including gmdate, simple arithmetic is not recommended. time() currently returns an integer decimal but if you go too far or too far back you may also see a float as it is not covered by the operating system's 32 BIT range. https://www.php.net/manual/en/datetime.gettimestamp.php

This is an example I hope it helps you


date_default_timezone_set("Europe/Helsinki");
$time = time(); //GMT+0000

function conversion_offset($time) {
$a = date('Y-m-d H:i:s Z', $time);
$len = strpos($a, ':');
$datetime = substr($a, 0, ($len + (6)));
$b = strtotime($datetime, $time);
$offset = (int) substr($a, ($len + (7)));

if($time > 0 && $offset <= 0)
$result = $time - ($offset);
elseif($time > 0 && $offset >= 0)
$result = $time + $offset;
elseif($time < 0 && $offset >= 0)
$result = $time + $offset;
else
$result = $time - ($offset);
return $result;
}
$time = strtotime('-1980-04-03 00:00:00');
$a = date('Y-m-d H:i:s Z', $time);
$b = gmdate('Y-m-d H:i:s Z', $time);
$date = date('Y-m-d H:i:s Z', conversion_offset($time));
var_dump($time, 'gmdate: ' . $b, 'conversion_offset: ' . $date, 'Local time: ' . $a);
 [2021-08-02 13:37 UTC] cmb@php.net
-Summary: Incredibly wrong and stupid note is highly ranked on `time()` docs +Summary: Unfortunate example on time() man page -Status: Open +Status: Verified -Type: Feature/Change Request +Type: Documentation Problem
 [2021-08-02 13:37 UTC] cmb@php.net
Yeah, right, the example #1 on that page is bad.  Using gmdate()
instead of date() would be an improvement, but still there would
be issues with leap seconds (which UTC supports).  And I agree
that such date/time calculations are generally problematic, if not
outright bad, so the user note makes sense.
 [2021-08-02 22:42 UTC] a at b dot c dot de
Some example that doesn't try to do date arithmetic by hand...

echo "The date is now ", date('F jS Y', time()), ". It has been for ", time() - strtotime('today'), " seconds so far, and will remain so for another ", strtotime('tomorrow') - time(), " seconds. It has been ", time(), " seconds since midnight January 1st 1970 GMT.";
 [2021-08-04 13:39 UTC] cmb@php.net
-Package: Website problem +Package: Date/time related
 [2021-08-04 13:39 UTC] cmb@php.net
> Some example that doesn't try to do date arithmetic by hand...

Nice!  Maybe you want to submit a pull request
(<https://github.com/php/doc-en/pulls>)?
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Oct 16 09:03:33 2021 UTC