note 125041 added to class.dateinterval
| From: | KEINOS@php.net | Date: | Sun, 24 May 2020 10:45:30 +0000 |
| Subject: | note 125041 added to class.dateinterval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214672@lists.php.net to get a copy of this message | ||
Note that you CAN NOT get the exact time in seconds from a \DateInterval object.
The reason is simple. While adding the properties up, you can not determine the days of the month.
<?php
function getTTL(\DateInterval $ttl): int
{
$secs = (isset($ttl->f) ? ($ttl->f): 0)
+ ($ttl->s)
+ ($ttl->i * 60)
+ ($ttl->h * 60 * 60)
+ ($ttl->d * 60 * 60 * 24)
+ ($ttl->m * 60 * 60 * 24 * 30) //Feb. is 28 days, 29 in an intercalary year
+ ($ttl->y * 60 * 60 * 24 * 365);
return intval(round($secs));
}
?>
So, keep in mind that \DateInterval is something to add-on to time.
<?php
function getTTL(\DateInterval $ttl): int
{
// Find seconds from it's current state of time
$secs = \DateTime::createFromFormat('U',
'0')->add($ttl)->format('U');
return intval(abs($secs));
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 103.51.166.36 (proxied: 122.219.245.20)
----
Manual Page -- http://php.net/manual/en/class.dateinterval.php
Edit -- https://master.php.net/note/edit/125041
Del: integrated -- https://master.php.net/note/delete/125041/integrated
Del: useless -- https://master.php.net/note/delete/125041/useless
Del: bad code -- https://master.php.net/note/delete/125041/bad+code
Del: spam -- https://master.php.net/note/delete/125041/spam
Del: non-english -- https://master.php.net/note/delete/125041/non-english
Del: in docs -- https://master.php.net/note/delete/125041/in+docs
Del: other reasons-- https://master.php.net/note/delete/125041
Reject -- https://master.php.net/note/reject/125041
Search -- https://master.php.net/manage/user-notes.php