php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73097 strftime %G does not work in some cases
Submitted: 2016-09-16 10:38 UTC Modified: 2016-09-16 15:12 UTC
From: dmanye at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: Irrelevant OS: linux
Private report: No CVE-ID: None
 [2016-09-16 10:38 UTC] dmanye at gmail dot com
Description:
------------
strftime(%G) for Jan 1st 2017 returns 2016 instead of 2017. 

trying with other years works as expected (2013..2015 and 2018..2020) but others do not (2005, 2006, 2010..2012, 2016, 2017). these lists are not exhaustive.

tested on 7.0.10 and 5.6.7 php versions.

the date function works as expected. 

here's a second 19-line script that looks for the first January day where %G works as expected:

-----------------
<?php

echo "php version: " . phpversion() . "\n";

for ($year=1970;$year<2030;$year++) {

	$day = 1;
	$seconds = mktime(0,0,0,1,$day,$year);
	while ( strftime("%G",$seconds) != $year ) {
		$day ++;
		$seconds = mktime(0,0,0,1,$day,$year);
	}

	if ($day == 1)
		echo "$year: ok\n";
	else
		echo "$year: it is not $year until " . strftime("%c",$seconds) . "\n";
}
----------------------------------

thanks.


Test script:
---------------
<?php

echo "php version: " . phpversion() . "\n";

$seconds = mktime(0,0,0,1,1,2017);
echo "seconds from epoch: $seconds\n";
echo "strftime(%G): " . strftime("%G",$seconds) . "\n";
echo "strftime(%c): " . strftime("%c",$seconds) . "\n";
echo "date(Y)     : " . date("Y",$seconds)      . "\n";
?>


Expected result:
----------------
php version: 7.0.10-1
seconds from epoch: 1483225200
strftime(%G): 2017
strftime(%c): Sun Jan  1 00:00:00 2017
date(Y)     : 2017

Actual result:
--------------
php version: 7.0.10-1
seconds from epoch: 1483225200
strftime(%G): 2016
strftime(%c): Sun Jan  1 00:00:00 2017
date(Y)     : 2017


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-09-16 15:12 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-09-16 15:12 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

http://php.net/manual/en/function.strftime.php
> %G	The full four-digit version of %g
> %g	Two digit representation of the year going by ISO-8601:1988 standards (see %V)
> %V	ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week

Compare with
> %Y	Four digit representation for the year
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 10:01:32 2024 UTC