Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #5040
| From | "Peter H. Coffin" <hellsop@ninehells.com> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: strtotime() not able to handle 04/29 correctly??? |
| Date | 2012-02-29 14:19 -0600 |
| Organization | A noiseless patient Spider |
| Message-ID | <slrnjkt22c.rcn.hellsop@nibelheim.ninehells.com> (permalink) |
| References | <a63a7827-bc1a-483a-96ae-de8289aa2de0@32g2000yqn.googlegroups.com> |
On Wed, 29 Feb 2012 10:41:34 -0800 (PST), cerr wrote:
> Hi,
>
> I calculate the amount of days between two dates like this:
>
> $start = strtotime($_SESSION['sdate']);
> $end = strtotime($_SESSION['hdate']);
> echo ($end - $start);
> $duration = ($end - $start) / 3600 /24;
> $duration+=1; //plus one day
> echo " The ad will show for ".$duration." day(s)<br/>";
>
> Which generally works fine but things seem to mess up when I have a
> start sdate of 2012-02-29 and an hdate of 2012-04-01. I get a duration
> of: 32.9583333333
> Why is this? That's messing up my calculations. Is there a better way
> to do this?
"Note:
Using this function for mathematical operations is not advisable. It is
better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later,
or DateTime::modify() in PHP 5.2."
Be that as it may...
What result are you expecting? 2012-02-29 to 2012-04-01 is 32 days isn't
it? Plus the 1 you're adding for no obvious reason would be 33, and
rounding the result you *did* get comes out 33..
--
"The last refuge of the insomniac is a sense of superiority to the
sleeping world."
--Leonard Cohen, The Favourite Game
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
strtotime() not able to handle 04/29 correctly??? cerr <ron.eggler@gmail.com> - 2012-02-29 10:41 -0800
Re: strtotime() not able to handle 04/29 correctly??? "Peter H. Coffin" <hellsop@ninehells.com> - 2012-02-29 14:19 -0600
Re: strtotime() not able to handle 04/29 correctly??? Jerry Stuckle <jstucklex@attglobal.net> - 2012-02-29 16:21 -0500
Re: strtotime() not able to handle 04/29 correctly??? Doug Miller <doug_at_milmac_dot_com@example.com> - 2012-02-29 23:24 +0000
Re: strtotime() not able to handle 04/29 correctly??? Eli the Bearded <*@eli.users.panix.com> - 2012-03-01 00:25 +0000
Re: strtotime() not able to handle 04/29 correctly??? The Natural Philosopher <tnp@invalid.invalid> - 2012-03-01 00:46 +0000
Re: strtotime() not able to handle 04/29 correctly??? Doug Miller <doug_at_milmac_dot_com@example.com> - 2012-03-01 01:28 +0000
csiph-web