我试图在两次linux时间之间取得差异。我希望在几个月内显示出差异。我从数据库中获取一个Linux时间并将其与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;
}
答案 0 :(得分:2)
使用DateTime
和DateInterval
个对象,这应该有效:
$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
根据我对您的代码的理解,您必须事先做到这一点:
$timestamp1 = time();
$timestamp2 = $status;