note 110586 added to function.mktime
| From: | JacobSantos | Date: | Fri, 09 Nov 2012 16:17:07 +0000 |
| Subject: | note 110586 added to function.mktime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-191827@lists.php.net to get a copy of this message | ||
Please note that incrementing a date using mktime in a loop is not proper. You could do it, except
that there is a far better method found in the DateTime PHP class. Look at the documentation for
DateTime::modify, DateTime::add (when supported) and DateTime::sub (when supported).
Also, adding seconds to a time is, well it isn't as easy as it seems, "Hey I'll just
add 3600 seconds or 86400 seconds or x seconds!". The phrase once bitten, twice shy is quite
applicable with the usage of adding seconds. If you ever had to 'fix' a time by
calculating midnight to add the correct number of seconds, then you are doing it wrong.
Luckily, knowing is not a requirement, because DateTime and friends exists, removing the complexity
for you.
So if given a choice of
mktime($seconds, $minutes, $hours+1);
and
$datetime->modify('+1 hour');
or
$datetime->add('P1H');
I'll go with the second choice, but probably not the third, unless I was using
DateInterval::createFromDateString, so that other developers knew my intent.
----
Server IP: 64.71.164.2
Probable Submitter: 72.22.219.5
----
Manual Page -- http://www.php.net/manual/en/function.mktime.php
Edit -- https://master.php.net/note/edit/110586
Del: integrated -- https://master.php.net/note/delete/110586/integrated
Del: useless -- https://master.php.net/note/delete/110586/useless
Del: bad code -- https://master.php.net/note/delete/110586/bad+code
Del: spam -- https://master.php.net/note/delete/110586/spam
Del: non-english -- https://master.php.net/note/delete/110586/non-english
Del: in docs -- https://master.php.net/note/delete/110586/in+docs
Del: other reasons-- https://master.php.net/note/delete/110586
Reject -- https://master.php.net/note/reject/110586
Search -- https://master.php.net/manage/user-notes.php