php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50019 DatePeriod doesn't accept negative intervals
Submitted: 2009-10-27 15:13 UTC Modified: 2017-01-11 17:02 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: jennifer dot kimball at nrc dot ca Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.3.0 OS: Solaris 10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
43 - 37 = ?
Subscribe to this entry?

 
 [2009-10-27 15:13 UTC] jennifer dot kimball at nrc dot ca
Description:
------------
DatePeriod should be able to create a list of dates that goes into the future from the start date, or into the past from the start date. There is nothing in the documentation to suggest this should not be possible, and trying it does not throw an error.

Given that a question like 'What are the dates of the previous 3 Saturdays' is fairly common, it seems sensible that DatePeriod be able to calculate that way.

Reproduce code:
---------------
---
From manual page: dateperiod.construct#Description
---
//postive interval works:
$d1=date_create('2004-12-25');
$i=DateInterval::createFromDateString('1 year');
$d2=date_create('2009-03-03');
$p=new DatePeriod($d1,$i,$d2);
foreach($p as $d) echo $d->format('Y-m-d');


//negative interval fails with no error
$d1=date_create('2009-12-25');
$i=DateInterval::createFromDateString('-1 year');
$d2=date_create('2004-03-03');
$p=new DatePeriod($d1,$i,$d2);
foreach($p as $d) echo $d->format('Y-m-d');

Expected result:
----------------
//postive interval output (which does happen)
2004-12-25
2005-12-25
2006-12-25
2007-12-25
2008-12-25

//negative interval output (which does not happen)
2009-12-25
2008-12-25
2007-12-25
2006-12-25
2005-12-25
2004-12-25

Actual result:
--------------
//no output
//no error

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-24 10:30 UTC] jani@php.net
-Package: Feature/Change Request +Package: Date/time related
 [2011-10-12 16:33 UTC] dagguh at gmail dot com
You are mistaken.

This is the faulty line:
$i=DateInterval::createFromDateString('-1 year');

DateInterval::createFromDateString doesn't report any error when it fails. Fix 
your DateInterval first :)
 [2016-06-12 10:48 UTC] cmb@php.net
Given that negative DateIntervals are allowed and date-time
arithmetic is supported[1], that appears to be a reasonable
request.

[1] <https://3v4l.org/ufYCh>
 [2017-01-11 17:02 UTC] heiglandreas@php.net
-Status: Open +Status: Not a bug
 [2017-01-11 17:02 UTC] heiglandreas@php.net
As the requested feature works pretty well when instantiating the DatePeriod-Object with start and end[1] and with start and recurrences[2]

When instantiating it with a start that is higher than the end it is intended behaviour that no output is generated.

I'm closing this issue therefore.


[1] https://3v4l.org/ucso1
[2] https://3v4l.org/GSOpu
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 13:01:32 2024 UTC