FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » strtotime() not able to handle 04/29 correctly???
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: strtotime() not able to handle 04/29 correctly??? [message #177221 is a reply to message #177220] Wed, 29 February 2012 20:19 Go to previous messageGo to previous message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma:
Senior Member
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Is there a way to distinguish an auto-refresh from a manual page load?
Next Topic: CrowdFinch Technologies
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Dec 01 03:50:36 GMT 2024

Total time taken to generate the page: 0.03634 seconds