简体   繁体   中英

PHP find difference between two linux time

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;

}

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM