Re: newbie: help with date arithmetic
| From: | Ernest E Vogelsinger | Date: | Tue, 12 Nov 2002 15:40:22 +0000 |
| Subject: | Re: newbie: help with date arithmetic | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-124010@lists.php.net to get a copy of this message | ||
At 16:30 12.11.2002, ROBERT MCPEAK spoke out and said:
--------------------[snip]--------------------
>I'm trying to add/subract two dates. I think I need to use mktime() but I
>can't quite figure out how.
>
>I'd like to do something like this:
>
>(2002-11-15)-(2002-11-10)=5
>
>or
>
>(2002-12-10)-(2002-11-10)=20
>
>Obviously taking into account number of days in a given month.
--------------------[snip]--------------------
:)) rtfm, again :))
1) http://www.php.net/manual/en/function.strtotime.php
2) http://www.gnu.org/manual/tar-1.12/html_chapter/tar_7.html
3) your second equation would return 30, not 20 ;->
4) try this:
/* untested */
function days($str_from, $str_to) {
$from = strtotime(isset($str_from) ? $str_from : 'now');
$to = strtotime(isset($str_to) ? $str_to : 'now');
if ($from > $to) {
$x = $from; $from = $to; $to = $x;
}
$rslt = $to - $from; // number in seconds
return (int)($rslt / 86400); // return days (1d = 86400 secs)
}
Hope this helps,
--
>O Ernest E. Vogelsinger
(\) ICQ #13394035
^ http://www.vogelsinger.at/