? Failure

User tests: Successful: Unsuccessful:

avatar izharaazmi
izharaazmi
21 Nov 2015

The current implementation to add to or subtract from a JDate object uses the DateTime class add() and sub() methods. These methods are not pretty straight forward as they require the argument to be of DateInterval instance type. This usually gets too complicated.

Here I try to offer new methods that would simplify the job by doing all the homework stuff internally and provide the programmer a straightforward way to do the same task.

Negative input values are automatically handled appropriately in both methods.

These methods allows chaining, therefore you can do inline operations as with most of other JDate methods.

Example:

To add 4 days to November 21, 2015 use:

$date = JFactory::getDate('2015-11-21');
$date->addInterval(4, 'd'); // You can also use 'day', 'days' instead

To subtract 3 months from November 21, 2015 use:

$date = JFactory::getDate('2015-11-21');
$date->subInterval(3, 'mo'); // You can also use 'month', 'months' instead

Before this patch you'd need following:

$date     = JFactory::getDate('2015-11-21');
$interval = new new DateInterval('P4D');
$date->add($interval);

and

$date     = JFactory::getDate('2015-11-21');
$interval = new new DateInterval('P3M');
$date->sub($interval);

Also negative values are not acceptable to these methods.

avatar izharaazmi izharaazmi - open - 21 Nov 2015
avatar izharaazmi izharaazmi - change - 21 Nov 2015
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 21 Nov 2015
Labels Added: ?
avatar mbabker
mbabker - comment - 21 Nov 2015

:-1:

It might be a nice shortcut method, but IMO it's coming at the expense of adding a rather complex internal method to build the DateInterval object and allows non-standard values to be used. 118 lines of untested code just to remove one extra line when handling intervals?

avatar izharaazmi
izharaazmi - comment - 21 Nov 2015

@mbabker The long method to create the DateInterval is there to support those variations to the 2nd argument to the addInterval and subInterval methods.
However, taking hint from your note "...and allows non-standard values to be used..." I think I should consult the php manual for strtotime or date or use DateInterval::createFromDateString

But using the existing method is definitely very tedious. Its not just about one extra line rather too much off effort just to add/subtract a value.

I see difference:

$date->add(new DateInterval('P' . $days . 'D'))->sub(new DateInterval('PT' . $hours . 'H'));

vs

$date->addInterval($days, 'd'))->subInterval($hours, 'h');

Please let me know what you suggest.

avatar izharaazmi
izharaazmi - comment - 21 Nov 2015

Further, I was thinking of these methods to be named plus() and minus() respectively. But I'm afraid that whether it sounds as good to others. Please advise.

avatar mbabker
mbabker - comment - 21 Nov 2015

My personal suggestion is don't add these methods. Your examples in the opening of this pull request show that working with DateInterval objects is not a complex operation. You have added a rather complex internal method to build DateInterval objects to JDate and this method accepts additional values that DateInterval does not (you allow 'd', 'days', and 'day' as alternatives to the required 'D' for a DateInterval object).

It's a nice shortcut, yes the code may be more compact and readable at a quick glance, but to me there is no benefit gained from adding this to the API.

avatar wilsonge
wilsonge - comment - 23 Nov 2015

I agree with @mbabker The code you gave as working at the moment is 1 extra line on top of this code but involves using the native PHP API rather than our own untested methods with custom formats. In view of this I'm going to close this PR at this time. And as you stated you can still have human readable code by using DateInterval::createFromDateString

avatar wilsonge wilsonge - change - 23 Nov 2015
Status Pending Closed
Closed_Date 0000-00-00 00:00:00 2015-11-23 11:13:18
Closed_By wilsonge
avatar wilsonge wilsonge - close - 23 Nov 2015
avatar wilsonge wilsonge - close - 23 Nov 2015
avatar izharaazmi
izharaazmi - comment - 23 Nov 2015

@mbabker @wilsonge Ok, I agree that this may be too much of internal code to reduce one single line.
Just as curiosity, I'd like to ask whether it woul'd be a good idea to allow this

$date->addInterval('P2D');

instead of the long approach of manually instantiating a DateInterval object every time like

$date->add(new DateInterval('P2D'));

However, I think this should have been the case with native PHP itself and not just Joomla.

avatar Bakual
Bakual - comment - 23 Nov 2015

I don't see the issue with using native PHP functions. Using $date->add(new DateInterval('P2D')); is perfectly fine and much better than $date->addInterval('P2D'); where another dev looking at your code first has to figure out what addInterval() does :smile:

avatar izharaazmi
izharaazmi - comment - 23 Nov 2015

Okay, thanks :)
I still hope someday PHP itself provides such shortcut. PHP has always been good at these :+1:

avatar izharaazmi izharaazmi - head_ref_deleted - 23 Nov 2015
avatar izharaazmi
izharaazmi - comment - 23 Nov 2015

Just one thing off topic with this. I need to discuss something that does not fit in like an issue or PR. So can you please suggest where can I start such a discussion?

avatar zero-24
zero-24 - comment - 23 Nov 2015
avatar izharaazmi
izharaazmi - comment - 4 Mar 2016

Note for anyone landing here later

Somehow I missed that the JDate::modify(string $modify) can be used for such cases with almost no extra effort.

See: DateTime::modify(string $modify)

Add a Comment

Login with GitHub to post a comment