Re: date weirdness

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2007-09-18 at 15:32 -0500, Greg Donald wrote:
> I have extracted a small portion of a calendar application I developed
> recently to show some strange behavior with the date() function.  When I
> run this I get duplicate dates occasionally down the list.  I'm seeing
> 11/4 twice for example.  Sometimes dates are missing from the list.  The
> results change from day to day.
> 
> #!/usr/bin/env php
> <?php
> 
> error_reporting( E_ALL );
> 
> define( 'SECONDS_IN_A_DAY', 60 * 60 * 24 );
> define( 'LAST_SUNDAY', strtotime( 'last Sunday' ) );
> 
> echo 'SECONDS_IN_A_DAY = ' . SECONDS_IN_A_DAY . "\n";
> echo 'LAST_SUNDAY = ' . LAST_SUNDAY . "\n";
> 
> for( $x = 0; $x < 365; $x++ )
> {
>     $seconds = LAST_SUNDAY + ( SECONDS_IN_A_DAY * $x );
>     $date = date( 'n/j', $seconds );
>     echo "$seconds $date\n";
> }
> ?>
>
> I get the same results with latest PHP4 and PHP5.

Using seconds is a hack that doesn't account for DST. Use the following
instead:

<?php

error_reporting( E_ALL );

define( 'LAST_SUNDAY', strtotime( 'last Sunday' ) );

echo 'LAST_SUNDAY = ' . LAST_SUNDAY . "\n";

for( $x = 0; $x < 365; $x++ )
{
    $timestamp = strtotime( "+$x days", LAST_SUNDAY );
    $date = date( 'n/j', $timestamp );
    echo "$timestamp $date\n";
}

?>

Cheers,
Rob.
-- 
...........................................................
SwarmBuy.com - http://www.swarmbuy.com

    Leveraging the buying power of the masses!
...........................................................

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux