0

I'm trying to get the difference between two linux times. I want to show the difference in months. I am getting one Linux time from the database and comparing it against time();.

$sql_sms_transactions = "SELECT MAX(`sendtime`) FROM `sms_transaction`  where customer_id='$customer_id'";

    $result_sms_transactions = mysql_query($sql_sms_transactions);
    while($row2=mysql_fetch_assoc($result_sms_transactions))
{

    $status = $row2['MAX(`sendtime`)'];

    //print $status;
    $month = date('m', $status); // 1-12
    $year = date('YW', $status);
    //print $month;
    //print $year;

    $current=time();
    $monthcurrent = date('m', $current); // 1-12
    $yearcurrent = date('YW', $current);

    //print $monthcurrent;
    //print $yearcurrent;

}
Luke Peterson
  • 8,584
  • 8
  • 45
  • 46
Xavi
  • 2,552
  • 7
  • 40
  • 59
  • possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Gergo Erdosi Jun 15 '14 at 09:53

1 Answers1

2

Using DateTime and DateInterval objects, this should work:

$date1 = new DateTime();
$date1->setTimestamp($timestamp1);

$date2 = new DateTime();
$date2->setTimestamp($timestamp2);

$diff = $date1->diff($date2);
// Result of type DateInterval
echo ($diff->y*12) + $diff->m

From what I understand of your code, you have to do this beforehand:

$timestamp1 = time();
$timestamp2 = $status;
julienc
  • 19,087
  • 17
  • 82
  • 82