Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts

    display 30 dates starting from today

    how can I print the last thirty dates. Starting from today:
    Example:
    2011 09 13
    2011 09 12
    ......
    2011 08 14


    Thanks..

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    PHP Code:
    for($i 0$i 30$i++)
        echo 
    date('Y m d'strtotime('today + ' $i ' day')), '<br />'

  • Users who have thanked Inigoesdr for this post:

    coding_begins (09-13-2011)

  • #3
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts
    I am printing the last 30 days starting from today.
    i want to print the current date and also the the day after the current date in the next line. The below code does not seem to work correctly..

    for($i = 0; $i < 30; $i++)
    {

    $d[$i]=strtotime('today - ' . $i . ' day');
    echo "D:".date('Y m d', $d[$i])."<br>";
    echo "D+1:".date('Y m d', $d[$i+1])."<br>";
    }

  • #4
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts
    I mean it odes not print the D+1 value correctly..

  • #5
    Regular Coder
    Join Date
    Jul 2010
    Posts
    271
    Thanks
    3
    Thanked 40 Times in 40 Posts
    You say the day after. Do you mean one more day back or one day forward?
    This shows the next day back, change the -1 to +1 if you want the other way.
    PHP Code:
    <?php
    for($i 0$i 30$i++)
    {
    $y=$i+1;
    $d[$i]=strtotime('today - ' $i ' day');
    $d1[$i]=strtotime('today - ' $y ' day');
    echo 
    "D:".date('Y m d'$d[$i])."<br>";
    echo 
    "D+1:".date('Y m d'$d1[$i])."<br>";
    }
    ?>
    Last edited by jimhill; 09-13-2011 at 07:25 PM. Reason: One mistake in the code
    If you can't stand behind your troops, feel free to stand in front of them
    Semper Fidelis

  • #6
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts
    for($i = 0; $i < 30; $i++)
    {

    $d[$i]=strtotime('today - ' . $i . ' day');
    $d1[$i]=strtotime('today - ' . $i+1 . ' day');
    echo "D:".date('Y m d', $d[$i])."<br>";
    echo "D+1:".date('Y m d', $d1[$i])."<br>";
    }

    It prints the same date again..it does not increment the value for D+1:
    D:2011 09 13
    D+1:2011 09 14
    D:2011 09 12
    D+1:2011 09 14
    D:2011 09 11
    D+1:2011 09 14
    D:2011 09 10
    ...

  • #7
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts
    never mind..i missed the y variable..

  • #8
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts
    But it prints the first value of D+1 as 1969-...
    for($i = 0; $i < 30; $i++)
    {
    $y=$i-1;
    $d[$i]=strtotime('today - ' . $i . ' day');
    $d1[$i]=strtotime('today - ' . $y . ' day');
    echo "D:".date('Y m d', $d[$i])."<br>";
    echo "D+1:".date('Y m d', $d1[$i])."<br>";
    }

    D:2011 09 13
    D+1:1969 12 31
    D:2011 09 12
    D+1:2011 09 13
    D:2011 09 11

  • #9
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Whoops. I misread what you asked for. Try this for the last 30 days:
    PHP Code:
    for($i 0$i 30$i++)
        echo 
    date('Y m d'strtotime('today' . ($i ' - ' $i ' day' ''))), '<br />';

    // Or
    echo date('Y m d'), '<br />';
    for(
    $i 1$i 30$i++)
        echo 
    date('Y m d'strtotime('today - ' $i ' day')), '<br />'

  • #10
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    I would have done this differently, but I want your opinion on whether it's better/worse than what's already been posted - or just different.
    PHP Code:
    // timestamp for right now
    $now time();

    // a day in seconds
    $day 60*60*24;

    // Start $i at $now timestamp, increment it by 1 day until it's 30 days (< 31)
    // For last 30 days, change += to -=
    for($i $now$i < ($now+($day*31)); $i += $day ){
        echo 
    date("d-m-Y",$i)."\n";


  • #11
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    That depends on your criteria for better or worse. Mine is shorter and easier to read, but yours might be faster(by a few ms, though you could optimize it some more). The output(other than the date format and newline) appears to be the same.

  • #12
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    My criteria was exactly on those comparisons, but I wasn't sure about the time. I didn't think there would be much in it, but I don't entirely know how the strtotime() actually parses the 'today' operand. Thanks for your input


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •