SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot darksystem's Avatar
    Join Date
    Jan 2005
    Location
    Fayettville, North Carolina
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date listing reverse

    Hi,

    I have this code that list the last 30 days

    Code:
    for($i=1378080000 ; $i <= time(); $i += 86400) {
    	$statsDate = date('Y-m-d', $i);
    	echo $statsDate.'<br>';
    }
    and I tried to reverse it back to put the latest date on top and this is the code

    Code:
    for($i=time(); $i>=1378080000 ; $i-=86400) {
    	$statsDate = date('Y-m-d', $i);
    	echo $statsDate.'<br>';
    }
    however, my problem on the reverse part, it doesnt include yesterday. only the day before yesterday down to the last 29 days.
    starting from the the day before yesterday.

    I hope you get me right.

    What I want is totally the reverse of the first code with the same output. Try the codes and you'll see what i mean.
    Ebay API, OSC/CRE/OscMax/ZenCart/SEO Services
    Lucki Multimedia - Email

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You really don't want to do it that way. You'll notice that your first block of code doesn't actually get to "today". Leap days could give you different results. Look at DateTime - and for your case, DateTime::add()
    - Robert

  3. #3
    SitePoint Zealot darksystem's Avatar
    Join Date
    Jan 2005
    Location
    Fayettville, North Carolina
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the 1st block of code does get today's date.

    if you noticed on $i <= time() on the for loop.

    basically that states that im getting date lesser or equal today's date.

    anyway, thanks for your opinion but i need a total reverse of the 1st block of code as mine doesnt work. it doesnt include today's date using the 2nd block of code that i created.

    any help is highly appreciated.
    Ebay API, OSC/CRE/OscMax/ZenCart/SEO Services
    Lucki Multimedia - Email

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What I was saying is that your code isn't always going to do what you think it is. Depending on the time of day, you won't get today's date in there. And daylight savings time changes will also affect the output (not leap days like I said at first - I meant DST).

    But if you want a reverse of what you have, this will absolutely give you that. It's not the most efficient but will get you what you're after. Just do the same loop and reverse the results after.
    PHP Code:
    $dates = array();
    for(
    $i=1378080000 $i <= time(); $i += 86400) {
      
    $dates[] = $statsDate date('Y-m-d'$i);
    }
    foreach( 
    array_reverse$dates ) as $d ) {
      echo 
    $d '<br>';

    - Robert


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
  •