PHP forum

Strtotime() not able to handle 04/29 correctly???

Feb 29, 2012 6:41 pm
Cerr

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

Feb 29, 2012 8:19 pm
Peter H. Coffin
Re: strtotime() not able to handle 04/29 correctly???

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
Feb 29, 2012 9:21 pm
Jerry Stuckle
Re: strtotime() not able to handle 04/29 correctly???

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@attglobal.net
==================
Feb 29, 2012 11:24 pm
Doug Miller
Re: strtotime() not able to handle 04/29 correctly???

cerr <ron.eggler@gmail.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.




Previous Thread: Name of page itself?
Next Thread: Javascript, XmlHttpRequest, PHP, and daemon

Related Forum Topics
The prefered way to handle mobile apps?
Hi,

I would like to display my front page diferently for mobile users, (as
well as a few other pages).

I see on the web that there are various scripts to detect mobile apps,
(from the user agent).

But from experience those quickly get out of date or are simply buggy
and return some...
OT: Blackberry won't display a simple table correctly
I wish I had a blackberry to test , but some users are saying this
table won't display correctly:

http://www.squashclub.org/test.php

Any smoking gun with html errors? Or is it a limitation with
blackberry ( I doubt it ).


Strtotime bug?
I'm not sure if this is a bug in strtotime() or not.

I'm using PHP Version 5.2.13 and my webmail client (written in PHP)
has problem displaying the dates on some emails.

Here's an example:

echo "Good:", strtotime("Wed, 11 Aug 2010 13:45:34 -0700"), "\n";
echo "Bad:", strtotime("Wed, 11...
Strtotime
Can anyone explain this to me:

shouldn't these to both return the same timestamp?

$ts = 1204351200;
strtotime('+1 month',$ts); // returns 1207026000
strtotime('@'.$ts.' +1 month')); // returns 1207029600... where's the
extra 1 hour coming from?