Skip to content

Bug #60774 (DateInterval::format("%a") is always zero when an interval i... #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ext/date/lib/parse_date.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Generated by re2c 0.13.5 on Mon Dec 5 22:02:27 2011 */
/* Generated by re2c 0.13.5 on Sun Aug 5 16:55:20 2012 */
#line 1 "ext/date/lib/parse_date.re"
/*
+----------------------------------------------------------------------+
Expand Down Expand Up @@ -24823,6 +24823,7 @@ timelib_time* timelib_strtotime(char *s, int len, struct timelib_error_container
in.tzdb = tzdb;
in.time->is_localtime = 0;
in.time->zone_type = 0;
in.time->relative.days = TIMELIB_UNSET;

do {
t = scan(&in, tz_get_wrapper);
Expand Down
1 change: 1 addition & 0 deletions ext/date/lib/parse_date.re
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,7 @@ timelib_time* timelib_strtotime(char *s, int len, struct timelib_error_container
in.tzdb = tzdb;
in.time->is_localtime = 0;
in.time->zone_type = 0;
in.time->relative.days = TIMELIB_UNSET;

do {
t = scan(&in, tz_get_wrapper);
Expand Down
30 changes: 30 additions & 0 deletions ext/date/tests/bug60774.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Bug #60774 (DateInterval::format("%a") is always zero when an interval is created using the createFromDateString method)
--FILE--
<?php
$i= DateInterval::createFromDateString('2 days');
var_dump($i);
echo $i->format("%d"), "\n";
echo $i->format("%a"), "\n";
?>
--EXPECT--
object(DateInterval)#1 (8) {
["y"]=>
int(0)
["m"]=>
int(0)
["d"]=>
int(2)
["h"]=>
int(0)
["i"]=>
int(0)
["s"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
bool(false)
}
2
(unknown)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand - why it's unknown if the string says "2 days"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The %a modifier is only for the total number of days between two dates. Since there is only 1 date we don't have a difference.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The %a modifier is only for the total number of days between two dates.
Since there is only 1 date we don't have a difference.

On Sun, Aug 5, 2012 at 11:37 PM, Stanislav Malyshev <
reply@reply.github.com

wrote:

  • int(0)
  • ["d"]=>
  • int(2)
  • ["h"]=>
  • int(0)
  • ["i"]=>
  • int(0)
  • ["s"]=>
  • int(0)
  • ["invert"]=>
  • int(0)
  • ["days"]=>
  • bool(false)
    +}
    +2
    +(unknown)

I'm not sure I understand - why it's unknown if the string says "2 days"?


Reply to this email directly or view it on GitHub:
https://github.com/php/php-src/pull/148/files#r1309145

Lonny Kapelushnik
http://lonnylot.com
(732) 807-5509

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's actually no dates at all in this example, just the interval. And I thought the whole point of the interval is that you don't need two dates - you just have relative interval, like "2 days interval". I don't see why %a there can't be 2 days.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I'm aware the %a is supposed to be the total number of days in the interval. I think you're right that if the interval is 2 days then %a could be set to 2. However, with other intervals, such as 1 month, you would not be able to set the %a unless the interval was relative to todays date.

Additionally, the example in the createfromdatestring docs say that instantiating from the constructor or createfromdatestring static method should create the same results. In bug 49778 Derick changed the the constructor so that %a would be (unknown). My patch makes the results of createfromdatestring match the constructor results.