note 121103 added to datetime.add
| From: | rnealxp at yahoo dot com | Date: | Fri, 19 May 2017 15:43:34 +0000 |
| Subject: | note 121103 added to datetime.add | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209078@lists.php.net to get a copy of this message | ||
What you can do with this function/method is a great example of the philosophy: "just because
you can do it doesn't mean you should". I'm talking about two issues: (1) the number
of days in the month which varies from months 1-12 as well as for month 2 which could be leap year
(or not); and then issue (2): what if there is the need to specify a large quantity of an interval
such that it needs to be re-characterized into broader-scoped intervals (i.e. 184 seconds ==> 3
minutes-4 seconds). Examples in notes elsewhere in the docs for this function illustrate both issues
and their undesired effects so I won't focus on them further. But how did I decide to handle?
I've gone with four "public" functions and a single "private" function, and
without giving you a bunch of code to study, here are their summaries...
1. function adjustYear(int $yearsAdj){ //you can pass in +/- value and I adjust year value by that
value but then I also call PHP's 'cal_days_in_month' function to ensure the day
number I have in my date does not exceed days in the month for the new year/month combo--if it does,
I adjust the day value downward.
2. function adjustMonth(int $monthsAdj){ //same notes as above apply; but also, I allow any number
to be passed in for $monthsAdj. I use the 'int' function (int($monthsAdj/12)) and modulus
% operator to determine how to adjust both year and month. And again, I use
'cal_days_in_month' function to tweak the day number as needed.
3. function addTime(int $days, int $hours, int $minutes, int $seconds){
// I use date_add and create a DateInterval object from the corresponding string spec (created from
the args passed to this function). Note that months and years are excluded due to the bad
side-effects already mentioned elsewhere.
4. function subtractTime(int $days, int $hours, int $minutes, int $seconds){
//notes for "addTime" also apply to this function but note that I like separate add and
subtract functions because setting the DateInterval property flag to indicate add/subtract is not as
intuitive for future coding.
5. function recharacterizeIntervals(int $days, int $hours, int $minutes, int $seconds){ // I convert
excessively large quantities of any one interval into the next largest interval using the
'int' function and modulus (%) operator. I then use the result of this function when
creating the string interval specification that gets passed when generating the DateInterval object
for calling the date_add function (or object-method equivalent).
**Results/goals...
--any number of days/hours/minutes/seconds can be passed in to add/subtractTime and all of
"Y/M/D/H/M/S" values get adjusted as you would expect.
--using adjustYear/Month lets you pass +/- values and only "Y/M" values get modified
without having undesirable effects on day values.
--a call to the "recharacterize" function helps ensure proper and desired values are in
the intervals prior to calling date_add to let it do its work.
----
Server IP: 72.52.91.14
Probable Submitter: 24.158.119.193
----
Manual Page -- http://php.net/manual/en/datetime.add.php
Edit -- https://master.php.net/note/edit/121103
Del: integrated -- https://master.php.net/note/delete/121103/integrated
Del: useless -- https://master.php.net/note/delete/121103/useless
Del: bad code -- https://master.php.net/note/delete/121103/bad+code
Del: spam -- https://master.php.net/note/delete/121103/spam
Del: non-english -- https://master.php.net/note/delete/121103/non-english
Del: in docs -- https://master.php.net/note/delete/121103/in+docs
Del: other reasons-- https://master.php.net/note/delete/121103
Reject -- https://master.php.net/note/reject/121103
Search -- https://master.php.net/manage/user-notes.php