The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 2 of 2

Thread: Subtracting Dates

  1. #1

    Subtracting Dates

    I want to go 7 days back from a certain date but when I do so the days go into negative?

    PHP Code:
    <?php
    $date 
    "09/01/09";
    $date strtotime( (string) $date );
    $day date('d'$date);
    $month date('F'$date);
    $year date('Y'$date);
    for (
    $i 1$i <= 7$i++) {
        echo 
    "$month$day$year<br />";
        
    $day--;
    }
    ?>
    output
    Code:
    September, 01, 2009
    September, 0, 2009
    September, -1, 2009
    September, -2, 2009
    September, -3, 2009
    September, -4, 2009
    September, -5, 2009

  2. #2
    PHP Code:
    <?php
    $date 
    "09/01/09";

    for (
    $i 0$i 7$i++) {
        
    $timestamp strtotime($date." -$i day");
        echo 
    date('m/d/y',$timestamp).'<br/>';
    }
    ?>
    this includes the "current" $date -6 days for a total of 7 displayed dates
    output:
    Code:
    09/01/09
    08/31/09
    08/30/09
    08/29/09
    08/28/09
    08/27/09
    08/26/09
    --------------

    PHP Code:
    <?php
    $date 
    "09/01/09";

    for (
    $i 1$i <= 7$i++) {
        
    $timestamp strtotime($date." -$i day");
        echo 
    date('m/d/y',$timestamp).'<br/>';
    }
    ?>
    this excludes the "current" date and only show the past 7
    output:
    Code:
    08/31/09
    08/30/09
    08/29/09
    08/28/09
    08/27/09
    08/26/09
    08/25/09
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012