php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76634 setISODate sets current time(date is correct)
Submitted: 2018-07-16 17:51 UTC Modified: 2018-07-17 07:45 UTC
From: bebd75kv6a at p6wsw dot anonbox dot net Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.0.30 OS: Ubuntu 16
Private report: No CVE-ID: None
 [2018-07-16 17:51 UTC] bebd75kv6a at p6wsw dot anonbox dot net
Description:
------------
I found out that setISODate function does not set timestamp as expected. It sets proper date but time seems to be taken from current timestamp. I know that it is meant to set date but it should set 00:00:00 for time part to ensure repeatable results.

Test script:
---------------
$date='2018-06-29 00:00:00';
$startDate=(new DateTime())->setISODate(date('Y',strtotime($date)),date('W',strtotime($date)))->format('Y-m-d H:i:s');
$endDate=(new DateTime())->setISODate(date('Y',strtotime($date)), date('W',strtotime($date)), 7)->format('Y-m-d H:i:s');
$startTimestamp = (new DateTime())->setISODate(date('Y',strtotime($date)),date('W',strtotime($date)))->getTimestamp();
echo 'input = '.$date;
echo '<br/>strtotime = '.strtotime($date);
echo '<br/>setISODate->getTimestamp = ';
echo $startTimestamp;
echo '</br>startDate = '.$startDate.'<br/>endDate = '.$endDate;

Expected result:
----------------
input = 2018-06-29 00:00:00
strtotime = 1530244800
setISODate->getTimestamp = 1529899200
startDate = 2018-06-25 00:00:00
endDate = 2018-07-01 00:00:00 

Actual result:
--------------
input = 2018-06-29 00:00:00
strtotime = 1530244800
setISODate->getTimestamp = 1529946301
startDate = 2018-06-25 13:05:01
endDate = 2018-07-01 13:05:01 

or bit later:

input = 2018-06-29 00:00:00
strtotime = 1530244800
setISODate->getTimestamp = 1529946704
startDate = 2018-06-25 13:11:44
endDate = 2018-07-01 13:11:44 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-07-17 00:58 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-07-17 00:58 UTC] requinix@php.net
setISODate() is meant to set the date, which is why it should not set the time as well.

Your problem is with the "new DateTime()" portion. That defaults to the current time. If you want something repeatable then you need to give it a fixed initial date.
https://3v4l.org/6hbDZ
 [2018-07-17 01:33 UTC] a at b dot c dot de
Or use DateTime::setTime() to set the time (same as if you used setDate to set the date).
 [2018-07-17 07:45 UTC] bebd75kv6a at p6wsw dot anonbox dot net
-: pawlu dot kolo at yahoo dot pl +: bebd75kv6a at p6wsw dot anonbox dot net
 [2018-07-17 07:46 UTC] bebd75kv6a at p6wsw dot anonbox dot net
ok
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC