Udders
Udders

Reputation: 6976

PHP working out minutes difference

I am working out the months, days, hours and minutes between two dates, I have successfully got it to work out the months, days and minutes, but I cannot for the life of get it to work out the minutes, below is my code.

<?php
    $date1 = "2012-07-01 00:00:00";
    $date2 = "2012-09-30 00:00:00";

    $diff = abs(strtotime($date2) - strtotime($date1));

    $years = floor($diff / (365*60*60*24));
    $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
    $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
    $minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24) / (60*60*24) / (60*60));

    printf("%d years, %d months, %d days, %d minutes\n", $years, $months, $days, $minutes);

?>

Upvotes: 0

Views: 752

Answers (3)

WWW
WWW

Reputation: 9860

Try reducing your $diff variable as you work everything else out:

$years = floor($diff / 31557600); // 31557600 = 365.25 * 86400
$diff -= $years * 31557600;
// I'd skip months, since different months have a different number of days.
// If you *really* want it, calculate the number of months from the days below.
$days = floor($diff / 86400); // 86400 = 24 * 3600
$diff -= $days * 86400;
$hours = floor($diff / 3600); // 3600 = 60 * 60
$diff -= $days * 3600;
$minutes = floor($diff / 60);
$diff -= $minutes * 60;
// $diff now equals the number of seconds left over

Alternatively, look into PHP's Date/Time object:

$date1 = new DateTime("2012-07-01 00:00:00");
$date2 = new DateTime("2012-09-30 00:00:00");
$diff = $date1->diff($date2); // $diff is a DateInterval object

Look at DateInterval->format() to determine how you want to format your output from $diff.

Upvotes: 1

nickb
nickb

Reputation: 59699

The best and most accurate way to approach this problem is to use the DateTime class. Otherwise you'll run into issues when you deal with anomalies in dates (leap year, etc).

$format = 'Y-m-d h:i:s';
$tz = new DateTimeZone('America/New_York');

// Create DateTime objs based on the above format
$t1 = DateTime::createFromFormat( $format, "2012-07-01 00:00:00", $tz);
$t2 = DateTime::createFromFormat( $format, "2012-09-30 00:00:00", $tz);

// Find the difference between them
$diff = $t1->diff( $t2);

// Print out the difference in each amount
$outputs = array( 'Y' => 'Year', 'm' => 'Month', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'seconds');

foreach( $outputs as $key => $value)
    echo $diff->format( '%'.$key) . ' ' . $value . "\n";

This outputs:

00 Year
2 Month
29 day
0 hour
0 minute
0 seconds

Upvotes: 1

Hrishikesh
Hrishikesh

Reputation: 1183

Used to have one of the following functions, to convert seconds into days/weeks/hh:mm:ss time etc. That should answer your query.

function duration ($sec, $padHours = false)  {
  if ($sec > 1209600) return "> ". intval(intval($sec) / 604800) . " weeks";
  if ($sec > 604800) return "1 week";
  if ($sec > 172800) return "> ". intval(intval($sec) / 86400) . " days";
  if ($sec > 86400) return "1 day";
  $hms = "";
  $hours = intval(intval($sec) / 3600);
  $hms .= ($padHours)
        ? str_pad($hours, 2, "0", STR_PAD_LEFT). ":"
        : $hours. ":";
  $minutes = intval(($sec / 60) % 60);
  $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ":";
  $seconds = intval($sec % 60);
  $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
  return $hms;

}

Upvotes: 0

Related Questions