SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    556
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Getting days from dates

    Hello,

    I have 2 dates lets say: 1-1-2005 and 5-1-2005. Now there are 5 days in those dates. I want to get the days from the 2 specified days.

    How do i do that ? Please help.

    Thanks.

  2. #2
    Woof! Woof! elel1138's Avatar
    Join Date
    Apr 2004
    Location
    Warsaw, Poland
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I understood correctly, you want to find how many days fit between two dates? Use the timestamp function- it converts any date to "number of seconds from January 1, 1970"; check the manual for mktime() function.

    PHP Code:
     //$date1, $date2 - dates to operate on

      
    $d1 date($date1,"d");
      
    $m1 date($date1,"m");
      
    $y1 date($date1,"y");

      
    // the same with date2

      //now, convert the dates to timestamps:
      
    $ts1 mktime(0,0,0,$d1$m1$y1);
      
    $ts2 mktime(0,0,0,$d2$m2$y2);

      
    //substract the two timestamps; use the abs function to avoid negative numbers
      
    $d abs($ts1-$ts2);

      
    //divide by the number of seconds in one day
      
    $days $d/86400;

      
    //et voila! 
    Leszek Leszczyński
    www.hazelsoft.com
    --- Real programmers don't document. If it was hard to write,
    it should be hard to understand. ---

  3. #3
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sVar_dateString1 '1-1-2005';
    $sVar_dateString2 '5-1-2005';

    $aVar_date1 explode('-'$sVar_dateString1);
    $aVar_date2 explode('-'$sVar_dateString2);

    $iVar_time1 mktime(0,0,0,$aVar_date1[1],$aVar_date1[0], $aVar_date1[2]);
    $iVar_time2 mktime(0,0,0,$aVar_date2[1],$aVar_date2[0], $aVar_date2[2]);

    $iVar_timeDifference $iVar_time2 $iVar_time1;
    $iVar_dayDifference = ($iVar_timeDifference / (60 60 24)) + 1;
    echo 
    $iVar_dayDifference
    The +1 is there since it seemed that you wanted it with both days inclusive.
    If you want the days between, excluding the boundry dates, make it - 1 instead.
    Hope that helps.

  4. #4
    Woof! Woof! elel1138's Avatar
    Join Date
    Apr 2004
    Location
    Warsaw, Poland
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was faster - however, the note about +1 is important, I somehow forgot to write it.
    Leszek Leszczyński
    www.hazelsoft.com
    --- Real programmers don't document. If it was hard to write,
    it should be hard to understand. ---

  5. #5
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hehe... yeah, I only saw your post after I posted.

  6. #6
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    556
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for the help. But isn't this solution good enough if it works ? I was trying this query but it does not seems to be working and i am getting an error instead:

    PHP Code:
    $q3 "SELECT DATEDIFF ('" $a1["sdate"] . " 23:59:59', '" $a1["fdate"] . "')";
                    
    $r3 mysql_query($q3) or die(mysql_error());
                    echo 
    mysql_result($r30); 

    The error i am getting is:

    You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '('2005-05-10 23:59:59', '2005-06-18')' at line 1
    Please help.

    Thanks

  7. #7
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check your MySQL version.
    The manual says that the DATEDIFF function was added only in version 4.1.1.
    So quite likely, the error is because your version is older.

  8. #8
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    556
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    How do i come to know which version i am using ?
    http://www.avissoftware.com/phpinfo.php will this help identify the version ?

    Thanks

  9. #9
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    556
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks silly me.

    Thanks again!

  10. #10
    SitePoint Zealot shaman's Avatar
    Join Date
    Feb 2005
    Location
    W.R. Home
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey, look to strtotime() too
    She's my german fraulein

  11. #11
    Woof! Woof! elel1138's Avatar
    Join Date
    Apr 2004
    Location
    Warsaw, Poland
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using the database is not a good solution. Why use a database (create connection, execute query, drop connection) and waste resources when you can do the same with 5 lines of code? It's against Occam's razor
    Leszek Leszczyński
    www.hazelsoft.com
    --- Real programmers don't document. If it was hard to write,
    it should be hard to understand. ---


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
  •