-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add DateInterval::add and DateInterval::sub methods #390
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1dd80b0
Add DateInterval::add
bukka 0239991
Add test for DateInterval::add
bukka 88824d4
Add DateInterval::sub
bukka ce75988
Support DateInterval invert operation for add and sub
bukka c6288e0
Fix invalid counting of inverted intervals when added or substracted
bukka 9d1e485
Remove useless normalization that is already done in timelib_do_rel_n…
bukka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--TEST-- | ||
Test DateInterval::add() basic functionality | ||
--CREDITS-- | ||
Jakub Zelenka <bukka@php.net> | ||
--SKIPIF-- | ||
<?php if (!method_exists('DateInterval', 'add')) die("skip: method doesn't exist"); ?> | ||
--FILE-- | ||
<?php | ||
date_default_timezone_set('UTC'); | ||
|
||
$date11 = new DateTime('2000-01-01 00:00:00'); | ||
$date12 = new DateTime('2000-01-10 22:30:23'); | ||
$interval1 = $date11->diff($date12); | ||
echo $interval1->format('%Y-%M-%D %H:%i:%s') . "\n"; | ||
|
||
$date21 = new DateTime('2000-01-01 00:00:00'); | ||
$date22 = new DateTime('2001-03-10 03:40:50'); | ||
$interval2 = $date21->diff($date22); | ||
echo $interval2->format('%Y-%M-%D %H:%i:%s') . "\n"; | ||
|
||
$interval = $interval1->add($interval2); | ||
echo $interval->format('%Y-%M-%D %H:%i:%s') . "\n"; | ||
|
||
$interval1->invert = 1; | ||
$interval = $interval->add($interval1); | ||
echo $interval->format('%Y-%M-%D %H:%i:%s') . "\n"; | ||
|
||
$i0 = new Dateinterval('P1Y4D'); | ||
$i1 = new DateInterval('P1Y3D'); | ||
$i0->invert = 1; | ||
|
||
$i2 = $i0->add($i1); /* -1 day */ | ||
echo $i2->format('%y-%m-%d %h-%i-%s') . "\n"; | ||
echo $i2->invert . "\n"; | ||
|
||
$i2 = $i0->sub($i1); /* -(2 years and 7 days) */ | ||
echo $i2->format('%y-%m-%d %h-%i-%s') . "\n"; | ||
echo $i2->invert . "\n"; | ||
|
||
$i0->invert = 0; | ||
$i2 = $i0->sub($i1); | ||
echo $i2->format('%y-%m-%d %h-%i-%s') . "\n"; | ||
echo $i2->invert . "\n"; | ||
|
||
$i0 = new Dateinterval('P1Y4DT3H'); | ||
$i1 = new DateInterval('P1Y4DT3H2M'); | ||
$i2 = $i0->sub($i1); /* -2 minutes */ | ||
echo $i2->format('%y-%m-%d %h-%i-%s') . "\n"; | ||
echo $i2->invert . "\n"; | ||
|
||
$i2 = $i1->sub($i0); | ||
echo $i2->format('%y-%m-%d %h-%i-%s') . "\n"; | ||
echo $i2->invert . "\n"; | ||
|
||
?> | ||
--EXPECT-- | ||
00-00-09 22:30:23 | ||
01-02-09 03:40:50 | ||
01-02-19 02:11:13 | ||
01-02-09 03:40:50 | ||
0-0-1 0-0-0 | ||
1 | ||
2-0-7 0-0-0 | ||
1 | ||
0-0-1 0-0-0 | ||
0 | ||
0-0-0 0-2-0 | ||
1 | ||
0-0-0 0-2-0 | ||
0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--TEST-- | ||
Test DateInterval::sub() basic functionality | ||
--CREDITS-- | ||
Jakub Zelenka <bukka@php.net> | ||
--SKIPIF-- | ||
<?php if (!method_exists('DateInterval', 'sub')) die("skip: method doesn't exist"); ?> | ||
--FILE-- | ||
<?php | ||
date_default_timezone_set('UTC'); | ||
|
||
$date11 = new DateTime('2000-01-01 00:00:00'); | ||
$date12 = new DateTime('2001-01-10 22:30:23'); | ||
$interval1 = $date11->diff($date12); | ||
echo $interval1->format('%Y-%M-%D %H:%i:%s') . "\n"; | ||
|
||
$date21 = new DateTime('2000-01-01 00:00:00'); | ||
$date22 = new DateTime('2000-03-10 03:40:50'); | ||
$interval2 = $date21->diff($date22); | ||
echo $interval2->format('%Y-%M-%D %H:%i:%s') . "\n"; | ||
|
||
$interval = $interval1->sub($interval2); | ||
echo $interval->format('%Y-%M-%D %H:%i:%s') . "\n"; | ||
|
||
?> | ||
--EXPECT-- | ||
01-00-09 22:30:23 | ||
00-02-09 03:40:50 | ||
00-10-00 18:49:33 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, no macros. They are a pain to debug and it's really annoying if their names are lower case too!