# Improve DateTime Class

- Thread: https://externals.io/message/64216
- Original archive: https://news-web.php.net/php.internals/64216
- Started by Christian Stoller on 2012-12-10T12:09:36+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #64216 — Christian Stoller — 2012-12-10T12:09:36+00:00

URL: https://externals.io/message/64216#64216

Hi internals,

what do you think about improving the modification functionality of the DateTime class. I always get a cold shiver, when I write something like this:
<?php
$date = new DateTime()
$date->modify(‘+15 day’);

In my opinion it would be nicer if one could write:
$date->modify(15, DateTime::INTERVAL_DAY); // for adding 15 days
$date->modify(-15, DateTime::INTERVAL_DAY); // for subtracting 15 days

Even better would be to have methods like addDays(), addMonths(), etc.

This would make it cleaner and more readable. You do not have to do something like this:

$date = new DateTime();
$date->modify(getDaysToAddMethod() . " day"); // I am not sure if a '+' sign is needed if the value is positive

And it is fully backward compatible.

Best regards
Christian

---

## #64217 — Marco Pivetta — 2012-12-10T12:20:18+00:00

URL: https://externals.io/message/64216#64217 · In reply to: #64216

Heya,

To me, an interval is well represented by DateInterval, while the
DateTime::modify() method looks more like a shortcut to it. Do you really
need the additional functionality? Shouldn't it be easier to $date->add(new
DateInterval('P15D')); ? It is also a better representation of an interval
to me...


Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

---

## #64220 — Sebastian Krebs — 2012-12-10T12:23:01+00:00

URL: https://externals.io/message/64216#64220 · In reply to: #64216

Hi,

are you mabe just looking for

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

?


2012/12/10 Christian Stoller <stoller@leonex.de>

> Hi internals,
>
> what do you think about improving the modification functionality of the
> DateTime class. I always get a cold shiver, when I write something like
> this:
> <?php
> $date = new DateTime()
> $date->modify(‘+15 day’);
>
> In my opinion it would be nicer if one could write:
> $date->modify(15, DateTime::INTERVAL_DAY); // for adding 15 days
> $date->modify(-15, DateTime::INTERVAL_DAY); // for subtracting 15 days
>
> Even better would be to have methods like addDays(), addMonths(), etc.
>
> This would make it cleaner and more readable. You do not have to do
> something like this:
>
> $date = new DateTime();
> $date->modify(getDaysToAddMethod() . " day"); // I am not sure if a '+'
> sign is needed if the value is positive
>
> And it is fully backward compatible.
>
> Best regards
> Christian
>
>


-- 
github.com/KingCrunch

---

## #64219 — Nikita Nefedov — 2012-12-10T12:23:58+00:00

URL: https://externals.io/message/64216#64219 · In reply to: #64216

On Mon, 10 Dec 2012 16:09:36 +0400, Christian Stoller <stoller@leonex.de>  
wrote:

> Hi internals,
>
> what do you think about improving the modification functionality of the  
> DateTime class. I always get a cold shiver, when I write something like  
> this:
> <?php
> $date = new DateTime()
> $date->modify(‘+15 day’);
>
> In my opinion it would be nicer if one could write:
> $date->modify(15, DateTime::INTERVAL_DAY); // for adding 15 days
> $date->modify(-15, DateTime::INTERVAL_DAY); // for subtracting 15 days
>
> Even better would be to have methods like addDays(), addMonths(), etc.
>
> This would make it cleaner and more readable. You do not have to do  
> something like this:
>
> $date = new DateTime();
> $date->modify(getDaysToAddMethod() . " day"); // I am not sure if a '+'  
> sign is needed if the value is positive
>
> And it is fully backward compatible.
>
> Best regards
> Christian
>

Hi,

I think we shouldn't improve this functionality because modifying date  
object in the first place is bad idea. Think about it as monatomic object,  
like integer, you can't create new integer object of 42 and then just  
change its value - it will be another object.

As a workaround you could create your own date class, extend it from  
\DateTime and add what you want.

---

## #64221 — Christian Stoller — 2012-12-10T12:35:28+00:00

URL: https://externals.io/message/64216#64221 · In reply to: #64220

Hm... I know '$date->add(new DateInterval('P15D'));' is possible, but it has the same problem.

I have to write:

$date = new DateTime()
$date->add(new DateInterval('P' . getDaysToAddMethod() . 'D'));

I think it is very hard to read. Or is it just my personal point of view?


@Nikita: I know what you mean. But how would you add a month to a DateTime?


Best regards
Christian


-----Original Message-----
From: sebastian.krebs.berlin@gmail.com [mailto:sebastian.krebs.berlin@gmail.com] On Behalf Of Sebastian Krebs
Sent: Monday, December 10, 2012 1:23 PM
To: PHP internals list
Subject: Re: [PHP-DEV] Improve DateTime Class

Hi,

are you mabe just looking for

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

?


2012/12/10 Christian Stoller <stoller@leonex.de>

> Hi internals,
>
> what do you think about improving the modification functionality of the
> DateTime class. I always get a cold shiver, when I write something like
> this:
> <?php
> $date = new DateTime()
> $date->modify(‘+15 day’);
>
> In my opinion it would be nicer if one could write:
> $date->modify(15, DateTime::INTERVAL_DAY); // for adding 15 days
> $date->modify(-15, DateTime::INTERVAL_DAY); // for subtracting 15 days
>
> Even better would be to have methods like addDays(), addMonths(), etc.
>
> This would make it cleaner and more readable. You do not have to do
> something like this:
>
> $date = new DateTime();
> $date->modify(getDaysToAddMethod() . " day"); // I am not sure if a '+'
> sign is needed if the value is positive
>
> And it is fully backward compatible.
>
> Best regards
> Christian
>
>


-- 
github.com/KingCrunch

---

## #64222 — Marco Pivetta — 2012-12-10T12:41:00+00:00

URL: https://externals.io/message/64216#64222 · In reply to: #64221

It's just a matter of getting used to it IMO. I am not sure if you can
simply modify its public properties, but if that's the case, that should
handle your problem.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

---

## #64228 — Daniel Ribeiro Gomes — 2012-12-10T13:38:44+00:00

URL: https://externals.io/message/64216#64228 · In reply to: #64222

I agree with Christian in the sense that the readability gets compromised.

But I also think that a custom DateTime class should solve the problem
properly, since readability is not a general requirement.

My solution for that problem would be to create that extension and have
those nice methods on it, but not to modify the core, though.

Like Nikita well said, DateTime object is not an object we can simply dive
into modifying it without having some really bad side effects.


Daniel Ribeiro Gomes Pereira
Twitter <https://twitter.com/#!/drgomesp> |
Facebook<https://www.facebook.com/profile.php?id=100000407054469>
 | LinkedIn <http://www.linkedin.com/pub/daniel-ribeiro-gomes/21/414/39>
iPhone: +55 (48) 9111-0931



2012/12/10 Marco Pivetta <ocramius@gmail.com>

> It's just a matter of getting used to it IMO. I am not sure if you can
> simply modify its public properties, but if that's the case, that should
> handle your problem.
>
> Marco Pivetta
>
> http://twitter.com/Ocramius
>
> http://ocramius.github.com/

---

## #64234 — keisial@gmail.com — 2012-12-10T19:44:45+00:00

URL: https://externals.io/message/64216#64234 · In reply to: #64221

> Hm... I know '$date->add(new DateInterval('P15D'));' is possible, but it has the same problem.
>
> I have to write:
>
> $date = new DateTime()
> $date->add(new DateInterval('P' . getDaysToAddMethod() . 'D'));
>
> I think it is very hard to read. Or is it just my personal point of view?
I agree it's unintuitive.

Rather than the original:

 $date->modify(15, DateTime::INTERVAL_DAY);

What about adding such option to DateInterval constructor?

new DateInterval(DateTime::INTERVAL_DAY, 15);

Or maybe: new DateInterval( array( DateTime::INTERVAL_DAY=> 15 ) );

---

## #64245 — Stas Malyshev — 2012-12-11T01:39:42+00:00

URL: https://externals.io/message/64216#64245 · In reply to: #64216

Hi!

> what do you think about improving the modification functionality of the DateTime class. I always get a cold shiver, when I write something like this:
> <?php
> $date = new DateTime()
> $date->modify(‘+15 day’);
> 
> In my opinion it would be nicer if one could write:
> $date->modify(15, DateTime::INTERVAL_DAY); // for adding 15 days
> $date->modify(-15, DateTime::INTERVAL_DAY); // for subtracting 15 days

I think there's a very low value in a new API that does exactly what
existing API does but in slightly different way because of tastes of
some particular person. If you want to add something that is not
possible with existing API, it is welcome (given RFC, etc.) but just
adding small variations on the same theme would only add confusion and
if needed, can be very easily implemented in userspace.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
