Results 1 to 5 of 5

Thread: get day duration from pair dates

  1. #1
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default get day duration from pair dates

    Hello im a newbie

    got a question

    i have 2 dates

    dateStart and dateEnd both stored as as type date: y-m-d (eg 2003-05-05)

    how do i get the day duration

    TQ
    Last edited by sabahan; 09-16-2009 at 02:23 AM.

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    PHP Code:
    <?php
    function dayDifference($date1$date2)
    {
        list(
    $date1Year$date1Month$date1Day) = explode('-'$date1);
        list(
    $date2Year$date2Month$date2Day) = explode('-'$date2);
        
    $time1 mktime(000$date1Month$date1Day$date1Year);
        
    $time2 mktime(000$date2Month$date2Day$date2Year);
        
    $diff abs($time1 $time2);
        
    $diff floor($diff / (60 60 24));
        return 
    $diff;
    }
    echo 
    dayDifference('2003-05-01''2004-05-15');

  3. #3
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    thanks techtiem...

    ok i'm a newbie..

    i have 2 dates dateStart and dateEnd

    i tried the code above like this

    PHP Code:
    $date1=$row['dateStart'];
    $date2=$row['dateEnd'];

    function 
    dayDifference($date1$date2)
    {
        list(
    $date1Year$date1Month$date1Day) = explode('-'$date1);
        list(
    $date2Year$date2Month$date2Day) = explode('-'$date2);
        
    $time1 mktime(000$date1Month$date1Day$date1Year);
        
    $time2 mktime(000$date2Month$date2Day$date2Year);
        
    $diff abs($time1 $time2);
        
    $diff floor($diff / (60 60 24));
        return 
    $diff;


    echo 
    dayDifference('$date1''$date2'); 
    and i got this warning

    Warning: mktime() expects parameter 6 to be long, string given in xx on line 31

    what do i need to change in this code ?
    PHP Code:

    echo dayDifference('2003-05-01''2004-05-15'); 
    above will return 380
    Last edited by sabahan; 09-16-2009 at 11:57 AM.

  4. #4
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    i tried echo this
    PHP Code:
    echo dayDifference('2003-05-01''2003-05-02'); 
    and it returns 1...should be 2


    echo this
    PHP Code:
    echo dayDifference('2003-05-01''0000-00-00'); 
    returns 1248

    ?
    PHP Code:
    echo dayDifference('0000-00-00''2003-05-02'); 
    1249

  5. #5
    Join Date
    Nov 2007
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    ok solved

    http://www.developertutorials.com/tu...018/page1.html

    here's how i do it for future ref

    PHP Code:
    function dateDiff($dformat$endDate$beginDate)
    {
    $date_parts1=explode($dformat$beginDate);
    $date_parts2=explode($dformat$endDate);
    $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
    $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
    return 
    $end_date $start_date 1;
    }



    $date1=date('m/d/Y'strtotime($row['dateStart']));
    $date2=date('m/d/Y'strtotime($row['dateEnd']));

    if (
    $row[dateEnd]=='0000-00-00')
     {
     echo 
    "1";
     }
    else
    {
        print 
    "If we minus " $date1 " from " $date2 " we get " dateDiff("/"$date2$date1) . ".";

    2 issue

    a. when i input dateStart only it returns

    If we minus 02/02/2003 from 01/01/1970 we get -12084.

    thats why i use the if/else condition

    b. and i change

    PHP Code:
    return $end_date $start_date
    to
    PHP Code:
    return $end_date $start_date 1
    so if date start is 2003-05-01 and dateEnd 2003-05-02 it should return 2 days

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
  •