note 124966 added to function.date-diff
| From: | contact-php at openflyers dot com | Date: | Thu, 30 Apr 2020 08:02:41 +0000 |
| Subject: | note 124966 added to function.date-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214562@lists.php.net to get a copy of this message | ||
<?php
/**
* We suppose that PHP is configured in UTC
* php.ini configuration:
* [Date]
* ; Defines the default timezone used by the date functions
* ; http://php.net/date.timezone
* date.timezone = UTC
* @link http://php.net/date.timezone
*/
/**
* getDaysBetween2Dates
*
* Return the difference of days between $date1 and $date2 ($date1 - $date2)
* if $absolute parameter is false, the return value is negative if $date2 is after than $date1
*
* @param DateTime $date1
* @param DateTime $date2
* @param Boolean $absolute
* = true
* @return integer
*/
function getDaysBetween2Dates(DateTime $date1, DateTime $date2, $absolute = true)
{
$interval = $date2->diff($date1);
// if we have to take in account the relative position (!$absolute) and the relative position is
negative,
// we return negatif value otherwise, we return the absolute value
return (!$absolute and $interval->invert) ? - $interval->days : $interval->days;
}
echo '<h3>2020-03-01 - 2020-02-01: 29 days as it\'s a standard leap
year</h3>';
echo getDaysBetween2Dates(new DateTime("2020-03-01"), new
DateTime("2020-02-01"), false);
echo '<h3>1900-03-01 - 1900-02-01: 28 days as it\'s a "standard"
century</h3>';
echo getDaysBetween2Dates(new DateTime("1900-03-01"), new
DateTime("1900-02-01"), false);
echo '<h3>2000-03-01 - 2000-02-01: 29 days as it\'s a century multiple of 400:
2000=400x5</h3>';
echo getDaysBetween2Dates(new DateTime("2000-03-01"), new
DateTime("2000-02-01"), false);
echo '<h3>2020-03-01 - 2020-04-01: -28 days as 2020-03-01 is before
2020-04-01</h3>';
echo getDaysBetween2Dates(new DateTime("2020-02-01"), new
DateTime("2020-03-01"), false);
?>
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.195 (proxied: 2a01:e0a:9:5390:acd2:b03a:152:f0d0)
----
Manual Page -- http://php.net/manual/en/function.date-diff.php
Edit -- https://master.php.net/note/edit/124966
Del: integrated -- https://master.php.net/note/delete/124966/integrated
Del: useless -- https://master.php.net/note/delete/124966/useless
Del: bad code -- https://master.php.net/note/delete/124966/bad+code
Del: spam -- https://master.php.net/note/delete/124966/spam
Del: non-english -- https://master.php.net/note/delete/124966/non-english
Del: in docs -- https://master.php.net/note/delete/124966/in+docs
Del: other reasons-- https://master.php.net/note/delete/124966
Reject -- https://master.php.net/note/reject/124966
Search -- https://master.php.net/manage/user-notes.php