There are two dates, how to determine the methods of php from one date to another in hours of days and months. I think a very common problem, tell me the algorithm.

I tried it myself through mktime - I got confused

  • What kind is your date? - Yoharny Babay
  • hh dd mm - 2 digits - Happy_Cougar

3 answers 3

Date and time

$date = new DateTime("2012-12-21"); $interval = $date->diff(new DateTime("now")); echo $interval->format(($interval->invert ? "Осталось" : "Прошло")." лет: %y; месяцев: %m; дней: %d; часов: %h; минут: %i; секунд: %s"); 
     $start = '12 02 12'; $end = '18 02 12'; $startExp = explode(' ', $start); $endExp = explode(' ', $end); $startTS = mktime(0, 0, 0, $startExp[1], $startExp[0], $startExp[2]); // timestamp начала $endTS = mktime(0, 0, 0, $endExp[1], $endExp[0], $endExp[2]); // timestamp конца var_dump($startTS, $endTS); 

    Next yourself.

    • @exec in the condition was “hours” “days” “months” and not “day” “month” “year” and I think the main problem was to parse the time interval back. - FLK
    • 2
      But. when I asked what kind of date, there was no time indicated. - Yoharny Babay
     function getTimeDifference($cur_time, $time) { $difference = abs($time – $cur_time); $return['days'] = floor($difference / 86400); $return['hours'] = floor($difference / 3600) % 24; $return['minutes'] = floor($difference / 60) % 60; return $return; } 

    Testing function:

     print_r(getTimeDifference(1242902278, 1242803038));