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
Switch to threaded view of this topic Create a new topic Submit Reply
strtotime() not able to handle 04/29 correctly??? [message #177220] Wed, 29 February 2012 18:41 Go to next message
cerr is currently offline  cerr
Messages: 33
Registered: September 2010
Karma: 0
Member
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?

Thanks!
Ron
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 next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
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
Re: strtotime() not able to handle 04/29 correctly??? [message #177222 is a reply to message #177220] Wed, 29 February 2012 21:21 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/29/2012 1:41 PM, 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?
>
> Thanks!
> Ron

Daylight savings time.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: strtotime() not able to handle 04/29 correctly??? [message #177223 is a reply to message #177220] Wed, 29 February 2012 23:24 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
cerr <ron(dot)eggler(at)gmail(dot)com> wrote in news:a63a7827-bc1a-483a-96ae-de8289aa2de0@
32g2000yqn.googlegroups.com:

> 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

which is 32 days 23 hours. What might throw this off by one hour? Hint: in the United States, it
happens every year on the second Sunday of March.
Re: strtotime() not able to handle 04/29 correctly??? [message #177224 is a reply to message #177223] Thu, 01 March 2012 00:25 Go to previous messageGo to next message
Eli the Bearded is currently offline  Eli the Bearded
Messages: 22
Registered: April 2011
Karma: 0
Junior Member
In comp.lang.php, Doug Miller <doug_at_milmac_dot_com(at)example(dot)com> wrote:
> which is 32 days 23 hours. What might throw this off by one hour? Hint:
> in the United States, it happens every year on the second Sunday of March.

For some values of "every year" and in some regions only. Arizona is
unaffected. Parts of Indiana, too. Years before 2007 don't have the
second Sunday of March significant at all.

Elijah
------
would rather do away with this whole daylight savings time business
Re: strtotime() not able to handle 04/29 correctly??? [message #177225 is a reply to message #177224] Thu, 01 March 2012 00:46 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Eli the Bearded wrote:

>
> Elijah
> ------
> would rather do away with this whole daylight savings time business

+1

--
To people who know nothing, anything is possible.
To people who know too much, it is a sad fact
that they know how little is really possible -
and how hard it is to achieve it.
Re: strtotime() not able to handle 04/29 correctly??? [message #177226 is a reply to message #177224] Thu, 01 March 2012 01:28 Go to previous message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
Eli the Bearded <*@eli.users.panix.com> wrote in news:eli$1202291918(at)qz(dot)little-
neck.ny.us:

> In comp.lang.php, Doug Miller <doug_at_milmac_dot_com(at)example(dot)com> wrote:
>> which is 32 days 23 hours. What might throw this off by one hour? Hint:
>> in the United States, it happens every year on the second Sunday of March.
>
> For some values of "every year"

Oh, really? You think the DST transition occurs only some years, and not others?

> and in some regions only. Arizona is unaffected.

Correct. Hawaii also.

> Parts of Indiana, too.

Incorrect. All of Indiana observes DST.

> Years before 2007 don't have the
> second Sunday of March significant at all.

Years before 2007 were not part of the OP's date range and thus are not relevant to this
discussion.
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Mon Nov 25 01:18:08 GMT 2024

Total time taken to generate the page: 0.02427 seconds