PHP date calculation

前端 未结 11 1578
不知归路
不知归路 2021-01-01 08:19

What is the best (date format independent way) in PHP to calculate difference in days between two dates in specified format.

I tried the following function:

相关标签:
11条回答
  • 2021-01-01 09:15

    The PEAR Date class offers all kinds of features for finding the differences between dates and about 1000 other things as well. The docs for it are here...

    0 讨论(0)
  • 2021-01-01 09:16

    The following works for me. Believe I found it on the php.net docs somewhere.

    *Edit - Woops, didn't see csl's post. This is the exact function from his link, must have been where I found it. ;)

    //Find the difference between two dates
    function dateDiff($startDate, $endDate)
    {
        // Parse dates for conversion
        $startArry = date_parse($startDate);
        $endArry = date_parse($endDate);
    
        // Convert dates to Julian Days
        $start_date = gregoriantojd($startArry["month"], $startArry["day"], $startArry["year"]);
        $end_date = gregoriantojd($endArry["month"], $endArry["day"], $endArry["year"]);
    
        // Return difference
        return round(($end_date - $start_date), 0);
    }
    
    0 讨论(0)
  • 2021-01-01 09:17

    Let's not overengineer, guys.

    $d1 = strtotime($first_date);
    $d2 = strtotime($second_date);
    $delta = $d2 - $d1;
    $num_days = ($delta / 86400);
    
    0 讨论(0)
  • 2021-01-01 09:18

    If you do not want or you cannot use Zend Framework or Pear package try this function i hope this would help:

    function dateDifference($date1, $date2)
    {
        $d1 = (is_string($date1) ? strtotime($date1) : $date1);
        $d2 = (is_string($date2) ? strtotime($date2) : $date2);
    
        $diff_secs = abs($d1 - $d2);
        $base_year = min(date("Y", $d1), date("Y", $d2));
    
        $diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);
    
        return array
        (
            "years"         => abs(substr(date('Ymd', $d1) - date('Ymd', $d2), 0, -4)),
            "months_total"  => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,
            "months"        => date("n", $diff) - 1,
            "days_total"    => floor($diff_secs / (3600 * 24)),
            "days"          => date("j", $diff) - 1,
            "hours_total"   => floor($diff_secs / 3600),
            "hours"         => date("G", $diff),
            "minutes_total" => floor($diff_secs / 60),
            "minutes"       => (int) date("i", $diff),
            "seconds_total" => $diff_secs,
            "seconds"       => (int) date("s", $diff)
        );
    }
    
    0 讨论(0)
  • 2021-01-01 09:18

    I was trying to calculate the difference of two dates for the purpose of showing the duration of an event. Most of the functions given on the problem fails if the event has a duration form friday at 17:00 to sunday at 15:00. My goal was to find the difference between the dates like:

    date('Ymd',$end)-date('Tmd',$begining);
    

    But that is likly to fail because there isn't 99 month in a year and 99 days in a month. I could convert the date string to UNIX timestamp and divide by 60*60*12, but some days have a greater or lesser number of hours, sometimes there's eaven a leap secound. So I made my own function using getdate() a function that returns an array of innformation about the timestamp.

    /*
     * datediff($first,$last)
     * $first - unix timestamp or string aksepted by strtotime()
     * $last - unix timestamp or string aksepted by strtotime()
     * 
     * return - the difference in days betveen the two dates
     */
    function datediff($first,$last){
     $first = is_numeric($first) ? $first : strtotime($first);
     $last  = is_numeric($last ) ? $last  : strtotime($last );
     if ($last<$first){
      // Can't calculate negative difference in dates
      return -1;
     }
     $first = getdate($first);
     $last =  getdate($last );
     // find the difference in days since the start of the year
     $datediff = $last['yday'] - $first['yday'];
     // if the years do not match add the appropriate number of days.
     $yearCountedFrom = $first['year'];
     while($last['year'] != $yearCountedFrom ){
      // Take leap years into account
      if( $yearCountedFrom % 4 == 0 && $yearCountedFrom != 1900 && $yearCountedFrom != 2100 ){
       //leap year
       $datediff += 366;
      }else{
       $datediff += 365;
      }
      $yearCountedFrom++;
     }
     return $datediff;
    }
    

    Concerning the GregorianToJD() function, it might work, but I feel a little bit uneasy since I do not understand how it work.

    0 讨论(0)
提交回复
热议问题