从当前日期和时间减去一些日期和时间,以在PHP中查找年龄 [英] Subtract some date and time from current date and time to find age in PHP

查看:92
本文介绍了从当前日期和时间减去一些日期和时间,以在PHP中查找年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一张票 $ create_time =2016-08-02 12:35:04。我想从当前日期和时间减去 $ create_time ,如 $ current_time =2016-08-02 16:16:02 3hr 41min 的格式查找年龄。

Suppose I have a ticket $create_time = "2016-08-02 12:35:04". I want to subtract the $create_time from current date and time like $current_time="2016-08-02 16:16:02" to find the age in the format 3hr 41min.

<?php

$sql = "SELECT count(*) as total, create_time FROM article where ticket_id='$ticket_id'";
$otrs_db = $this->load->database('otrs',true);
$result = $otrs_db->query($sql);

foreach($result->result() as $row)
{?>
    <div class="pull-left">
        <h4><b><?php echo $row->total ;?> Article(s)</b></h4>
    </div>
    <div class="pull-right">
        <h4>Age: <?php echo date("Y-m-d H:i", strtotime("-$row->create_time",strtotime($thestime))) ?>Created: <?php echo $row->create_time; ?></h4>
    </div>

<?php
}
?>

我知道我的日期减法代码是错误的。我如何做到这一点?

I know my date subtraction code is wrong. How can i do it right?

推荐答案

你应该使用 DateTime 课程。

You should use DateTime class.

$create_time = "2016-08-02 12:35:04";
$current_time="2016-08-02 16:16:02";

$dtCurrent = DateTime::createFromFormat('Y-m-d H:i:s', $current_time);
$dtCreate = DateTime::createFromFormat('Y-m-d H:i:s', $create_time);
$diff = $dtCurrent->diff($dtCreate);

echo $diff->format("%Y-%m-%d %H:%i");

这将返回 00-0-0 03:40
请参阅 DateInterval :: format 了解更多格式细节。

This returns 00-0-0 03:40 See DateInterval::format for more formatting details.

这篇关于从当前日期和时间减去一些日期和时间,以在PHP中查找年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆