SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Mar 2002
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question about date calculation

    Hello. When I create a variable that tells me today is December 13, 2003
    how do I use php date functions to calculate when it's
    3 days later? or 5?

    I want to calculate when a date is x days later than the original variable, which tells me today's date

    The reason is I am going to prevent downloads after x day (such as 3 days after file is created - today) but I am confused on how to do this

    Thanks

  2. #2
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $day3 =  strtotime("+3 days");
    echo 
    strftime("%m %d %y")."<br />";
    echo 
    strftime("%m %d %y"$day3); 
    This should give you an idea. Just adjust for your use of it. You can see if you just run above you get 2 dates 3 days apart.
    Erh

  3. #3
    SitePoint Guru
    Join Date
    Mar 2002
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THanks. That is very helpful. I also took the time to do a search and found something simlar.

    If I may, how do I do this with hours? Such as
    $var1 = 'today, noon'
    $var2 = '48 hours later'

    And calculate? I'll try to find out, but I figured I might as well ask, if that's ok

    Thanks

  4. #4
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll want to store a timestamp (http://php.net/time), and then use date("format", $row['timestamp']) for output.

    Then to check it, you go:

    if (time() > $row['timestamp']-60*60*24*3)
    {
    // It's past 3 days.. old time minus 60 seconds, times 60 minutes, times 24 hours, times 3 days is less than the current time
    }

  5. #5
    SitePoint Guru
    Join Date
    Mar 2002
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. It's just the idea of a timestamp confuses me.

    If I pull out the 'originaldate' variable from the db, and it's value is
    December 12, 2003 4:45 PM

    I can pull that value out with the format you used, and it would know how to calculate if it is now 40 hours later?

    That's why I am confused. I am learning this, and adding hours to this moment's time I can do, but to get a static value and manipulate it is tough. I'll try...

    Tx

  6. #6
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the SQL query: UNIX_TIMESTAMP(your_column) gives you a UNIX timestamp, then you can add 40 hours to it with 60 * 60 * 40.

    PHP Code:
    // Example query
    $sql    'SELECT UNIX_TIMESTAMP(foo) FROM bar WHERE id = 10';
    $result mysql_query($sql) or die(mysql_error());
    $row    mysql_fetch_array($result);

    // Example time handling
    echo 'Time + 40 hours: ' date('F j, Y, g:i a'$row['foo'] + 60 60 40); 


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
  •