SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date today yesterday etc

    Hey guys,

    I'm trying to get my code to display dates for today, yesterday and up to 7 days back in total. I have this code but can only make it go forward 3 days, not back. Any way I can do this?
    PHP Code:
    $currentday date('d'time());
    for ( 
    $i $currentday$i $currentday+3$i++ ){
    $day date('d'mktime('00''00''00'$month$i$year));
    $date date('j'mktime('00''00''00'$month$i$year));
    echo 
    "DAY: $date<br><br>";


  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i try to avoid mktime() like the plague. no particular reason, i just don't like the "look" of it. luckily i've never had to use it.

    seems like a regular time()-stamp and some math will do here. this does from 7 days in the past to 3 days in the future:

    PHP Code:
    <?php

    $now 
    time();

    for (
    $i = -7$i 4; ++$i)
    {
        
    // 86400 = seconds in a day
        
    $day  date('d'$now 86400 $i);
        
    $date date('j'$now 86400 $i);

        echo 
    "DAY: $date<br /><br />\n";
    }

    ?>
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Larry, I did some editing with your code to get the way I needed it. I ended up with this.
    PHP Code:
    $now time(); 
    for (
    $i 0$i 7; ++$i

        
    // 86400 = seconds in a day 
        
    $day  date('d'$now 86400 $i); 
        
    $date date('j'$now 86400 $i); 
        echo 
    "DAY: $date<br /><br>"



Bookmarks

Posting Permissions

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