php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22976 gmdate/date and strtotime/mktime not consistant
Submitted: 2003-03-31 05:39 UTC Modified: 2003-04-02 02:25 UTC
From: scochrane at mackaysstores dot co dot uk Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 4.3.0 OS: NT4 Server
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: scochrane at mackaysstores dot co dot uk
New email:
PHP Version: OS:

 

 [2003-03-31 05:39 UTC] scochrane at mackaysstores dot co dot uk
I have 2 Servers - 1 has Automatic Daylight Saving on, the other has it off. Both Servers have same PHP version and O/S (NT4 Server).

When I use this:
echo gmdate("Y-m-d", mktime("last Sunday")); // gmdate

it returns:
2003-03-30 on Server 1 and
2003-03-31 on Server 2

-------------------------------

when I use this:
echo date("Y-m-d", mktime("last Sunday")); // no gmdate!

it returns:
2003-03-31 on Server 1 and
2003-03-31 on Server 2

-------------------------------

When I use this:
echo gmdate("Y-m-d", gmmktime("last Sunday")); // gmdate and gmtime!

it returns:
2003-03-31 on Server 1 and
2003-03-30 on Server 2

-------------------------------

when I use this:
echo date("Y-m-d", gmmktime("last Sunday")); // gmmktime!

it returns:
2003-03-31 on Server 1 and
2003-03-30 on Server 2


So I have used every combination and cannot get both Servers to return the correct date. I need it to return 2003-03-30 on both Servers - as it should.

p.s. I cannot just change the 'Automatic Daylight' configuration as I need it to run in over 250 seperate Servers (they may have different OS settings)!

Cheers,
Stuart

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-31 05:51 UTC] mgf@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

Neither mktime() nor gmmktime() understands arguments like "last Sunday" -- please go and re-read the documentation for those functions.  If you have any further questions, please ask them on the php-general list.
 [2003-03-31 08:21 UTC] scochrane at mackaysstores dot co dot uk
By substituting mktime() with strtotime() has the same problem.

On the strtotime page of the manual - one of the examples given states 'echo strtotime ("last Monday"), "\n";' - so I beleave my sytax is valid.

With my script ammended to be valid (as below) I think this is a bug.

function last_sunday() {
	return gmdate("Y-m-d", strtotime("Last Sunday"));
}
echo "Last Sunday is returned as: ".last_sunday();

With Daylight saving on the above = 2003-03-29
With Daylight saving off the above = 2003-03-30

I have also use the GMT keyword with strtotime(), but this fails also.
 [2003-03-31 09:41 UTC] rasmus@php.net
strtotime() uses the local timezone adjusted time unless you stick "GMT" at the end.  Try this:

echo date("H:i:s D M d, Y",strtotime("Last Sunday GMT"));
echo date("H:i:s D M d, Y",strtotime("Last Sunday"));

I am in PDT and right now this gives me:

16:00:00 Sat Mar 29, 2003
00:00:00 Sun Mar 30, 2003

which shows that strtotime adjusts for GMT correctly.  Midnight last Sunday in GMT was actually Saturday in my timezone as GMT is 8 hours ahead of me, so it gave me 4pm Saturday.  And when I pass it "Last Sunday" without the GMT, which means I am asking for midnight last sunday in my own timezone, I get exactly that.
 [2003-03-31 09:56 UTC] scochrane at mackaysstores dot co dot uk
Thanks for your reply- that failed also though, I tracked down the error - it seems that systems that have 'Automatically adjust Daylight Saving' switch on will return the wrong date.

I managed to get both exceptions to work using:

if(date("I") == 1){
 // Daylight Saving is ON
 return gmdate("Y-m-d", mktime("Last Sunday"));
} else {
 // Daylight Saving is OFF
 return date("Y-m-d", strtotime("Last Sunday"));
}

echo last_sunday();
 [2003-03-31 10:01 UTC] rasmus@php.net
Your code makes no sense.  mktime() does not understand "Last Sunday", so if this code fixed your problem, then you have a whole bunch of other problems.
 [2003-04-02 02:25 UTC] scochrane at mackaysstores dot co dot uk
Fair point - my solution should not work (but it does).

Here is more details of the problem:
using your code, I ran it with both exceptions.

1. Automatically adjust clock for daylight saving changes = ON
[code]
echo "with GMT: ".date("H:i:s D M d, Y",strtotime("Last Sunday GMT"));
echo "<br>without GMT: ".date("H:i:s D M d, Y",strtotime("Last Sunday"));
[/code]

returns:
with GMT: 23:00:00 Sat Mar 29, 2003
without: 23:00:00 Sat Mar 29, 2003

2. Automatically adjust clock for daylight saving changes = OFF
[code]
echo "with GMT: ".date("H:i:s D M d, Y",strtotime("Last Sunday GMT"));
echo "<br>without GMT: ".date("H:i:s D M d, Y",strtotime("Last Sunday"));
[/code]

returns:
with GMT: 00:00:00 Sun Mar 30, 2003
without: 00:00:00 Sun Mar 30, 2003

So you see the GMT does nothing!!!
And there is no way to have the same script run with both exceptions.

Am I the only one to have encountered this problem?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 01:01:33 2024 UTC