Php时间戳在月份更改时显示负值

时间:2017-01-03 12:22:41

标签: php opencart

在Php控制器代码'date_added'已经存在,我为当前日期创建了'date_sla'。在我的外出单独两个都工作正常,但当我用来计算差异结果是在这个月内罚款。当月份更改时显示负数,如(3-1-2017) - (31-12-2016)= -28天

foreach ($results as $result) {
    $action = array();

    $action[] = array(
                'text' => $this->language->get('text_view'),
                'href' => $this->url->link('sale/order/info', 'token=' . $this->session->data['token'] . '&order_id=' . $result['order_id'] . $url, 'SSL')
            );

    if (strtotime($result['date_added']) > strtotime('-' . (int)$this->config->get('config_order_edit') . ' day')) {
        $action[] = array(
                    'text' => $this->language->get('text_edit'),
                    'href' => $this->url->link('sale/order/update', 'token=' . $this->session->data['token'] . '&order_id=' . $result['order_id'] . $url, 'SSL')
                );
    }

    $this->data['orders'][] = array(
                'order_id'      => $result['order_id'],
                'customer'      => $result['customer'],
                'status'        => $result['status'],
                'total'         => $this->currency->format($result['total'], $result['currency_code'], $result['currency_value']),
                'date_added'    => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
                'date_sla'    => date($this->language->get('date_format_short'), time()),
                'date_modified' => date($this->language->get('date_format_short'), strtotime($result['date_modified'])),
                'selected'      => isset($this->request->post['selected']) && in_array($result['order_id'], $this->request->post['selected']),
                'action'        => $action
            );
}

Html代码

<td class="right"><?php echo $order['date_sla'] - $order['date_added']." days";?></td>

1 个答案:

答案 0 :(得分:4)

首先回复$order['date_sla']$order['date_added']并查看它打印的内容,如果您的日期格式与您的句子中提到的格式(3-1-2017)那么请尝试这样:

$d1 = new DateTime('3-1-2017');
$d2 = new DateTime('31-12-2016');
$difference = $d2->diff($d1);
echo $difference->format('%r%a days');// 3 days

来自图片的日期格式示例:

$t=explode("-",'03/01/2017 - 05:23:23 AM');//replace 03/01/2017 - 05:23:23 AM with your variable $order['date_sla'] like $t=explode("-",$order['date_sla']);
$str=str_replace("/", "-", $t[0]);
$t1=explode("-",'31/12/2016 - 05:23:23 AM');//replace 03/01/2017 - 05:23:23 AM with your variable $order['date_added' like $t=explode("-",$order['date_added');
$str1=str_replace("/", "-",$t1[0]);

$d1 = new DateTime($str);
$d2 = new DateTime($str1);
$difference = $d2->diff($d1);
echo $difference->format('%r%a days');// 3 days