Results 1 to 4 of 4

Thread: Date subtraction in days

  1. #1

    Thread Starter
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141

    Date subtraction in days

    How do I find the difference between two dates in days?

    E.g. 08/01/2004 - 05/01/2004 = 3 days

    The actual dates to be used will be in MySQL datetime format (yyyy-mm-dd), but I don't think that really matters for this question.

    I'm sure this should be really simple, but I just can't figure it out!

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    I'm guessing you should convert both to the UNIX timestamp, and then take the less recent away from the more recent.. although I have no idea what functions you would use, I'm sure there are some that would work fine.. First, you could try jdtounix() (Julian Day to Unix), or you could just try using the MySQL function "UNIX_TIMESTAMP", like:

    Code:
    SELECT UNIX_TIMESTAMP(field) FROM table
    hmm, I'm not actually sure of what any of these will actually return, but maybe it can help you out or get you started.. I'll look around when I get home.

    edit: After looking at the PHP.net time() function, I looked up "mktime()", which converts a MySQL time to a UNIX time stamp..

    check out mktime(), http://ca2.php.net/manual/en/function.mktime.php. If it doesn't work for you, check out gmmktime() too.
    Last edited by kows; Jan 8th, 2004 at 03:40 PM.
    Like Archer? Check out some Sterling Archer quotes.

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    ok, hmm, I found what you needed.. it was on PHP.net under checkdate()

    PHP Code:
    //moon at justmoon dot de
    //07-Dec-2003 01:18 

    function diff_days($start_date$end_date){
       return 
    floor(abs(strtotime($start_date) - strtotime($end_date))/86400);
    }

    // You can use whatever days you want
    echo diff_days("20031013""20040105"); // result: 84

    // You can use every format strtotime supports
    echo diff_days("now""-1 year");

    // You can mix start and end
    echo diff_days("20031203""20031201"); // result: 2 
    Hope that helps you out.

    http://ca2.php.net/manual/en/function.checkdate.php
    Like Archer? Check out some Sterling Archer quotes.

  4. #4

    Thread Starter
    Addicted Member mralston's Avatar
    Join Date
    Aug 2002
    Location
    Altrincham Nr Manchester, England
    Posts
    141
    Thanks very much.

Posting Permissions

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



Click Here to Expand Forum to Full Width