PHP Date Difference Function?

Discussion in 'PHP' started by cancer10, May 24, 2008.

  1. #1
    Hi

    Why is not there any date difference function in the PHP library? Where they have plenty of other small functions why dont they have a basic date difference function as this is a very useful and important function?

    I know there are such functions over the internet if you google but you dont found this function in their official php.net site.

    Do you know why?
     
    cancer10, May 24, 2008 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    The reason is that PHP uses unix timestamps as their "native" time format. There's no reason for date comparison functions because you can use strtotime on any valid date string and do math on the integer it returns ...
     
    krakjoe, May 25, 2008 IP
  3. JRBHosting

    JRBHosting Peon

    Messages:
    121
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah...it doesn't take much...

    
    $date1 = "..";
    $date2 = "..";
    $difference = strtotime($date2)-strtotime($date1);
    
    Code (markup):
    I'm pretty sure (without looking it up) that the Zend Framework has a date time differential calculator built in, as do some other frameworks.

    Jason
     
    JRBHosting, May 25, 2008 IP
  4. Awesome Ninja

    Awesome Ninja Peon

    Messages:
    141
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Just create 2 timestamps, and subtract them. then convert the timestamp into a formatted date.
     
    Awesome Ninja, May 25, 2008 IP
  5. TeraTask

    TeraTask Peon

    Messages:
    37
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    PHP isn't a framework. It's a language. A framework would provide such a function, but given the ease of calculation and the lack of benefit for writing a PHP function to do what can be done in just a couple lines of code, I see no reason why it should be part of the overhead of the operating system.

    That said, you could always post your own version on the site's help.
     
    TeraTask, May 25, 2008 IP
  6. TeraTask

    TeraTask Peon

    Messages:
    37
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Oh, these 2 functions might help for a bit more specific time variance:

    
    <?php
    function numDays($date_1,$date_2) {
      $start_day = strtotime(date('m/d/Y 00:00:00',strtotime($date_1)));
      $end_day = strtotime(date('m/d/Y 23:59:59',strtotime($date_2)));
    
      $difference = abs($end_day - $start_day);
      return ceil($difference/(60*60*24));
    }
    echo numDays('1/1/08','1/3/08').'<br />';
    echo numDays('2/28/08','3/1/08').'<br />';
    echo numDays('1/1/08','12/31/08').'<br />';
    echo numDays('1/1/07','12/31/08').'<br />';
    
    
    function numMonths($date_1, $date_2) {
      $smallest_date = min(strtotime($date_1),strtotime($date_2));
      $smallest_year = date('Y',$smallest_date);
      $smallest_month = date('m',$smallest_date);
    
      $largest_date = max(strtotime($date_1),strtotime($date_2));
      $largest_year = date('Y',$largest_date);
      $largest_month = date('m',$largest_date);
    
      $years_between = $largest_year - $smallest_year - 1;
      $months_from_smallest = 13 - $smallest_month;
    
      if ($largest_year == $smallest_year) {
        return $largest_month - $smallest_month + 1;
      } else {
        return $years_between*12 + $months_from_smallest + $largest_month;
      }
    }
    echo numMonths('11/15/2008','2/1/2009').'<br />';
    echo numMonths('2/15/2008','11/1/2008').'<br />';
    ?> 
    
    PHP:
    Actually, I just added these to the php.net/date site.
     
    TeraTask, May 25, 2008 IP