SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to find the difference b/w two dates

    hi

    how to find the difference b/w two dates in php.

    the results of date(...) have to be compared and I'd like to know the difference in terms of dates.

    Thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That depends on what format you get the date in. If you get it in something like this: 2007-03-17 then you need to convert it to a unix date with the strtotime() function.

    Once that is done you will have the dates as number of seconds since January 1 1970 00:00:00 GMT

    You can then compare the two dates and figure out how many seconds there are between the two dates.

    Then it is simple math.

    $mins_between_dates = $secs_between_dates/60;
    $hours_between_dates = $secs_between_dates/3600;
    $days_between_dates = $secs_between_dates/86400;
    Soren Beck Jensen

    http://www.automaticbacklinks.com - Free link exchange network
    http://www.notwebdesign.com - Joomla web design in Spain

  3. #3
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that would depend somewhat on what kind of timestamp you have? epoch? mysql?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  4. #4
    SitePoint Addict ruba's Avatar
    Join Date
    Apr 2005
    Location
    Amman -Jordan
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.php.net/manual/en/function.date.php
    Code:
    function DatesBetween($startDate, $endDate){
       // get the number of days between the two given dates.
       $days = (strtotime($endDate) - strtotime($startDate)) / 86400 + 1;
       $startMonth = date("m", strtotime($startDate));
       $startDay = date("d", strtotime($startDate));
       $startYear = date("Y", strtotime($startDate));    
       $dates;//the array of dates to be passed back
       for($i=0; $i<$days; $i++){
           $dates[$i] = date("n/j/Y", mktime(0, 0, 0, $startMonth , ($startDay+$i), $startYear));
       }
       return $dates;    
    }
    
    
    example: $dates = DatesBetween("2/27/2007","3/3/2007")
    
    Array ( [0] => 2/27/2007 [1] => 2/28/2007 [2] => 3/1/2007 [3] => 3/2/2007 [4] => 3/3/2007 )
    Open Blocked website
    Open Blocked Website
    Knowledge Is Knowing That A Tomato Is A Fruit,
    Wisdom Is Not Putting It In A Fruit Salad.

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql

  6. #6
    SitePoint Addict ruba's Avatar
    Join Date
    Apr 2005
    Location
    Amman -Jordan
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Open Blocked website
    Open Blocked Website
    Knowledge Is Knowing That A Tomato Is A Fruit,
    Wisdom Is Not Putting It In A Fruit Salad.


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
  •