I'm Having some problems calculating the number of Days between Two Dates.. The dates are in YYYY-mm-dd (MySQL) Format, and I need to know the number of DAYS between the two.

I know this is simple... Just really hate DATE() and mktime() - They always confuse me..

    Then use the MySQL DATEDIFF() function HERE

    DATEDIFF(expr1,expr2)

    DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

      Need to do it in PHP.. DATEDIFF() was added in MySQL 4.1.1 and this is a 3.2x series

        Try this:

        $now_ts = strtotime($now);
        $then_ts = strtotime($then);
        $days_diff = 0;
        while ($now_ts > $then_ts) {
            $now_ts = strtotime('-1 day', $now_ts);
            $days_diff++;
        }

        or this:

        $now_arr = explode('-', $now);
        $then_arr = explode('-', $then);
        $now_jd = cal_to_jd(CAL_GREGORIAN, $now_arr[1], $now_arr[2], $now_arr[0]);
        $then_jd = cal_to_jd(CAL_GREGORIAN, $then_arr[1], $then_arr[2], $then_arr[0]);
        $days_diff = $now_jd - $then_jd;

          I use this nice little function:

          function timeLeft($theTime) {
          
          $now            = strtotime("now");
          $timeLeft    = $theTime - $now;
          
          if($timeLeft > 0) {
              $days = floor($timeLeft/60/60/24);
              $hours = $timeLeft/60/60%24;
              $mins = $timeLeft/60%60;
              $secs = $timeLeft%60;
          
              if($days) {
                  $theText    = $days . " Day(s)";
                  if($hours) {
                      $theText    .= ", " .$hours . " Hour(s) ";
                  }
              } elseif($hours) {
                  $theText    = $hours . " Hour(s)";
                  if($mins) {
                      $theText    .= ", " .$mins . " Minute(s) ";
                  }
              } elseif($mins) {
                  $theText    = $mins . " Minute(s)";
                  if($secs)
                  {
                      $theText    .= ", " .$secs . " Second(s) ";
                  }
              } elseif($secs) {
                  $theText    = $secs . " Second(s)";
              }
          } else {
              $theText    = "0";
          }
          
          return $theText;
          } 
          

          works great

            Write a Reply...