gpt4 book ai didi

PHP 找到两个 linux 时间之间的差异

转载 作者:太空宇宙 更新时间:2023-11-04 09:38:19 26 4
gpt4 key购买 nike

我试图找出两个 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;

}

最佳答案

使用 DateTimeDateInterval对象,这应该有效:

$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;

关于PHP 找到两个 linux 时间之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24228401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com