【问题标题】:PHP find difference between two linux timePHP查找两个linux时间之间的差异
【发布时间】:2014-08-05 09:00:53
【问题描述】:

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

}

【问题讨论】:

标签: php mysql linux datetime time


【解决方案1】:

使用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;

【讨论】:

  • 感谢您的回复,它不工作它什么都没有显示,您能更改有关我的代码的代码吗
  • 你设置$timestamp1=time()$timestamp2了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
  • 2015-03-19
  • 2022-12-20
  • 2014-02-02
  • 2012-02-09
  • 1970-01-01
相关资源
最近更新 更多