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
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
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 daysEven 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 positiveAnd it is fully backward compatible.
Best regards
Christian
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 daysEven 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 positiveAnd it is fully backward compatible.
Best regards
Christian
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
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 |
Facebookhttps://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
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 ) );
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 daysEven 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 positiveAnd 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.
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