php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68495 Weekday calculated incorrectly using $weekDay = date('N', strtotime($date));
Submitted: 2014-11-25 06:25 UTC Modified: 2014-11-25 08:36 UTC
From: 8f181bd2 at opayq dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.5Git-2014-11-25 (Git) OS: Windows
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: 8f181bd2 at opayq dot com
New email:
PHP Version: OS:

 

 [2014-11-25 06:25 UTC] 8f181bd2 at opayq dot com
Description:
------------
Weekday calculated incorrectly for dates in the far future, using $weekDay = date('N', strtotime($date));

Test script:
---------------
	function getWeekday($date)
	{
		$dateTime = \DateTime::createFromFormat('m/d/Y', $date);
		$date = date_format($dateTime, 'm/d/Y');
		
		$weekDay = date('N', strtotime($date));
		return $weekDay;
	}

	$arr = ['01/04/2050' => 'Sun',
		'01/09/2050' => 'Tues',
		'07/04/2050' => 'Mon',
		'04/06/2014' => 'Sun',
		'10/22/2014' => 'Wed',
		'10/25/2014' => 'Sat',
		'11/23/2014' => 'Sun'];

	foreach($arr as $date => $shouldBe)
	{
		echo $date.' is on weekday '.getWeekday($date).' - This should be '.$shouldBe.'<br/>';
	}

Expected result:
----------------
01/04/2050 is on weekday 0 - This should be Sun
01/09/2050 is on weekday 2 - This should be Tues
07/04/2050 is on weekday 1 - This should be Mon
04/06/2014 is on weekday 7 - This should be Sun
10/22/2014 is on weekday 3 - This should be Wed
10/25/2014 is on weekday 6 - This should be Sat
11/23/2014 is on weekday 7 - This should be Sun

Actual result:
--------------
01/04/2050 is on weekday 4 - This should be Sun
01/09/2050 is on weekday 4 - This should be Tues
07/04/2050 is on weekday 4 - This should be Mon
04/06/2014 is on weekday 7 - This should be Sun
10/22/2014 is on weekday 3 - This should be Wed
10/25/2014 is on weekday 6 - This should be Sat
11/23/2014 is on weekday 7 - This should be Sun

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-11-25 06:39 UTC] 8f181bd2 at opayq dot com
-: chanceofwonder at gmx dot com +: 8f181bd2 at opayq dot com -Summary: Weekday calculated incorrectly using $weekDay = date('N', strtotime($date)); +Summary: Exact PHP version
 [2014-11-25 06:39 UTC] 8f181bd2 at opayq dot com
Exact PHP version is 5.5.11 (built: Apr  8 2014 15:07:14)
 [2014-11-25 06:40 UTC] 8f181bd2 at opayq dot com
-Summary: Exact PHP version +Summary: Weekday calculated incorrectly using $weekDay = date('N', strtotime($date));
 [2014-11-25 06:40 UTC] 8f181bd2 at opayq dot com
(edited title)
 [2014-11-25 08:36 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-11-25 08:36 UTC] requinix@php.net
Congratulations, you've discovered the limitations of using 32-bit integers for timestamps.
http://en.wikipedia.org/wiki/Year_2038_problem

Unfortunately 64-bit builds for PHP on Windows are currently experimental so there is only one good solution: use just the DateTime family of classes for dealing with dates in the far future, as they do not have the problem that haunts the "normal" functions like strtotime() and date().
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 20 13:01:34 2024 UTC