php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #25787 'yday' in getdate() function is out by one day
Submitted: 2003-10-08 04:21 UTC Modified: 2004-07-05 12:24 UTC
From: australia at bookrealm dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.1 OS: SuSE Linux 8.2
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: australia at bookrealm dot com
New email:
PHP Version: OS:

 

 [2003-10-08 04:21 UTC] australia at bookrealm dot com
Description:
------------
The array key 'yday' from getdate() is one day less than it 
should be. Bug can be confirmed using mcal_day_of_year(). 
 
 

Reproduce code:
---------------
<?php
$y = date(Y);
$m = date(n);
$d = date(j);
echo mcal_day_of_year($y,$m,$d) . "<br />";

$day_year = getdate();
echo $day_year[yday];
?>

Expected result:
----------------
Example date: 
2003-10-8 
 
Expected result: 
281 
281 

Actual result:
--------------
Example date: 
2003-10-8 
 
Actual result: 
281 
280 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-10-08 04:51 UTC] sniper@php.net
Day count for this date (2003-10-08) is 280.

e.g. 
# php -r 'echo date("z");'
280

It's propably mcal that is broken. (and I mean the library, not extension)

 [2003-10-08 04:52 UTC] sniper@php.net
And RTFM: yday == 0-366 days

 [2003-10-08 05:30 UTC] australia at bookrealm dot com
Month / days (2003) 
 
Jan: 31 
Feb: 28 
Mar: 31 
Apr: 30 
May: 31 
Jun: 30 
Jul: 31 
Aug: 31 
Sep: 30 
TOTAL: 273 days 
 
Currently 8th of Oct, 2003, thus 
PLUS 8 days (273 + 8). So today is day: 281 
 
Thus, in my humble opinion, mcal_day_of_year() seem 
correct, getdate() seems not to be. But, if getdate() is 
correct, then that makes all the calendars and organizers, 
paper and computer, incorrect when they state Oct 8, 2003, 
as being day '281' and not '280'. 
 
I did "RTFM" as you had suggested. "0-366" just indicates 
the number range, correct? 
 
Cheers, 
Andrew Skripshak
 [2003-10-08 05:56 UTC] australia at bookrealm dot com
And, IMHO, the example you gave, 
echo date("z"); 
thus also seems to give incorrect results.
 [2003-10-08 06:05 UTC] sniper@php.net
Maybe this clears it for you:
# php -r 'echo date("z", mktime(1,1,1,1,1,2003));'
0

The count starts from 0..

 [2003-10-08 06:27 UTC] australia at bookrealm dot com
Yes, I figured the count started from zero, but really it 
shouldn't. The 1st of the 1st of any year should be day # 
1, not day # 0. 
 
The manuals state that 'yday' in getdate() and 'z' in 
date() give "The day of the year". This is not correct if 
both are out by one day, and no indication that that is the 
case, and no indication that you must add one day to make 
them correct. 
 
The proper fix would be to fix the code, but a slacker fix 
would be to state in the manuals that you must add a day, 
ie., date("z") + 1, to get the correct "day of the year" 
number. 
 
Up to you, of course, which you choose to do. 
 
Cheers, 
Andrew Skripshak
 [2003-10-08 06:37 UTC] sniper@php.net
There is no bug and this is how it works in libc provided date() too.

 [2003-10-08 06:43 UTC] sniper@php.net
I meant localtime, not date:

# man 3 localtime

And look at the tm_yday section.


 [2003-10-08 20:31 UTC] australia at bookrealm dot com
Ok, I'll work with it the way it is. 
 
However, there are two things, maybe in other places too, 
which need to be changed. The values in "Example returned 
values" for both date("z") and 'yday' in getdate() show 
"0-366", these should be changed to "0-365" (as is shown in 
the 'man 3 localtime' you gave me to look at). Else there 
be 366 days in a normal year, and 367 in a leap year? 
 
I still believe it should be "1-366", but ah well. 
 
Cheers, 
Andrew Skripshak
 [2003-10-08 20:42 UTC] australia at bookrealm dot com
It is interesting though, that 'tm_mon' shows "0-11", but 
date() and getdate() use "1-12".
 [2004-07-05 12:24 UTC] nlopess@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2020-02-07 06:12 UTC] phpdocbot@php.net
Automatic comment on behalf of nlopess
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=bd57b3fc033c24d950cb8a9099ef8b083dfda21b
Log: fix #25787: wrong yday range
 [2020-12-30 11:59 UTC] nikic@php.net
Automatic comment on behalf of yuw
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=90120f8fabddffb64b570497358d042acdb2f3ae
Log: fix #25787: wrong yday range
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Aug 05 14:00:03 2025 UTC