Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts

    difference between two dates

    how would i get the current date on an apache system and comapre it to another date. So i could find out if it was in the past, or how long until that date?

    e.g. today (12/11/2003) and another date (15/11/2003) would show three days remaining.

    i know how to do ti in javascript but would like to do it in php.


    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    in PHP there is a date command that you could use just take the current dates day, month, and year and subtract the other dates and check the result. If the result is positive then you know its in the future, negative is the past.

    Jason

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You should use mktime to get your 2 timestamps and then compare them and see which one is bigger. As for as getting the difference between 2 dates, like saying it's 2 years and 3 month and 5 days difference, there is not any built in function, you can get the years difference and then do the rest by comparing month and days and ....

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I stole some code from http://be2.php.net/date

    -------
    aprilchong at hotmail dot com
    22-Oct-2003 09:36
    This cals different days between given date and today.
    PHP Code:
    function daysDiff($y1$m1$d1) {
        
    $y2 date("Y");
        
    $m2 date("m");
        
    $d2 date("d");

        if (
    mktime(0,0,0,$m1,$d1,$y1) - mktime(0,0,0,date("m"),date("d"),date("Y"))<0) {
            
    $diff "expiried";
        } else {
            
    $y $y1 $y2;
            
    $m $m1 $m2;
            
    $d $d1 $d2;
            
    $daysInMonth date("t",$m1);
            if (
    $d<0) {$m--;$d=$d+$daysInMonth;}
            if (
    $m<0) {$y--;$m=$m+12;}

            if (
    $y>0)    $diff .= $y=="1 year " "$y years ";
            if (
    $m>0)    $diff .= $m==1"1 month and " "$m months and ";
            if (
    $d>0$diff .= $d=="1 day " "$d days ";
            
    $diff .= " remaining";
        }
        return 
    $diff;

    April White.
    ----------------------
    It computes the difference till today, but i suppose you can give it a twist to compute the difference between two dates.

    I personally never had to do it outside SQL, which is realy easy.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for your code, it looks very adaptable. I will play around with it and post back.

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks people, i have come up with the code to produce how long until fom deaqdlines are.

    PHP Code:
    <?php

    function daysDiff($y1$m1$d1$event$link) {
    $diff="";
        
    $y2 date("Y");
        
    $m2 date("m");
        
    $d2 date("d");

        if (
    mktime(0,0,0,$m1,$d1,$y1) - mktime(0,0,0,date("m"),date("d"),date("Y"))>0) {
           
            
    $y $y1 $y2;
            
    $m $m1 $m2;
            
    $d $d1 $d2;
            
    $daysInMonth date("t",$m1);
            if (
    $d<0) {$m--;$d=$d+$daysInMonth;}
            if (
    $m<0) {$y--;$m=$m+12;}

            if (
    $y>0)    $diff .= $y=="1 year " "$y years ";
            if (
    $m>0)    $diff .= $m==1"1 month and " "$m months and ";
            if (
    $d>0$diff .= $d=="1 day " "$d days ";
            
    $diff .= " remaining   ";
        
    $diff .=$event;
        
    $diff .=$link;
        
    $diff .= "<br>";
        }
        
    //return $diff;
    echo $diff;

    }
    daysDiff(2003,11,15"chips""  <a href='#dud'>link text</a>");
    daysDiff(2003,11,14"dfgfd""  <a href='#dud'>link text</a>");
    }
    ?>
    thanks
    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by raf

    I personally never had to do it outside SQL, which is realy easy.
    Sorry, I know that it might look a little bit outta topic, but would you please write the SQL statement you use to get 2 dates difference?? I only know DATE_SUB that can only subtract a year from the specified year, or a month from the specified month, or a combination of 2 factors, like Y_M, but to the best of my memory it doesn't have something like Y_M_D that gives the difference.

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    but would you please write the SQL statement you use to get 2 dates difference??
    That's quite impossible becuse there are bout 50 possible statements, depending on if you have a timestamp or datatime and depending on what you want the output to look like; number of days, number of months, number of years (or seconds etc)

    There are numerous mySQL datatimefunctions that return datediffernces, like datediff (where you can specify the unit), period_dif (returns number of months) and of course the to_days (returns days from 01/01/0000) which you can use to first get the num of days for two dates and then substract them and proces further.

    You might wanna read
    http://www.mysql.com/doc/en/Date_and...functions.html
    and specially the 'user comments' at the bottom, which contain code to compute various datedifferences, like how to compute the number of days, months and years between two dates.


  •  

    Posting Permissions

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