Solved

PHP date problem

Posted on 2004-10-22
221 Views
Last Modified: 2006-11-17
Hi, I have a small problem with dates.
I try to display a week in a small loop.
I start from one day, and add seven times one day. When I try this at the end of october, the result is not what I expect.

Here is a small exmaple program:

      1 <?php
      2     // Week 44 (end of october) in 2004
      3
      4     $date = 1099173600;
      5
      6     // display the whole week
      7     for ($i=0; $i<7; $i++)
      8     {
      9         echo (date ('Y-m-d', $date + ($i*24*60*60)));
     10         echo ('<br>');
     11     }
     12 ?>

And the output:

2004-10-31
2004-10-31
2004-11-01
2004-11-02
2004-11-03
2004-11-04
2004-11-05

The first and the second date appear to bet the same (although their integer representation isn't!)

Any help appreciated

Barry
0
Question by:barrel
    6 Comments
     
    LVL 27

    Expert Comment

    by:Diablo84
    replace:

    for ($i=0; $i<7; $i++)

    with:

    for ($i=1;$i<=7;$i++) {
    0
     
    LVL 27

    Expert Comment

    by:Diablo84
    eg:

    $date = 1099173600;

    for ($i=1;$i<=7;$i++) {
     echo (date ('Y-m-d', $date + ($i*24*60*60)));
     echo ('<br>');
     }

    otherwise i in the first loop is 0 so the timestamp will be $date+0 basically... or in otherwords identical to the timestamp
    0
     
    LVL 27

    Accepted Solution

    by:
    Actually with your original code, i cannot recreate the output you show, this however is a better way of writing this:

    <?php
    $date = 1099173600;

    for ($i=0;$i<7;$i++) {
     echo date('Y-m-d',strtotime("+$i days",$date))."<br>\n";
    }
    ?>
    0
     
    LVL 2

    Author Comment

    by:barrel
    Hi,

    Thanks for your _fast_ answer.
    The fact that I start at zero is because I calculate the first day of the week which also needs to be displayed.

    Your approach works for the specific date, but then I run into troubles when displaying a 'normal' week...

    The fact that you cannot reproduce it makes me think that this is a PHP bug?

    I am using version 4.3.4 by the way

    Barry
    0
     
    LVL 2

    Author Comment

    by:barrel
    Well... that did the trick.

    No idea why simply adding it doesn't work and the "+$i days" does...:-/

    Anyway, I am a happy man, I can continue... :-)
    0
     
    LVL 27

    Expert Comment

    by:Diablo84
    There are various issues that come up when working with timestamps and php, a lot of the time its related to DST (Daylight Savings time) which can throw it out an hour and hence produce the wrong stamp. When adding time in blocks of days, weeks, months etc its always best to use strtotime in my opinion because then it works around this problem.

    Best Wishes

    |)iablo
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now