计算两个数据时间之间的差异 [英] Calculate difference between two datetimes

查看:191
本文介绍了计算两个数据时间之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP和MySQL,并希望计算两个数据时间之间的日期时间差异。我有一个消息表,在该表中 createdate 是一个字段。我想从2月2日前的格式 1天前找出与当前日期的日期和时间差异

I am using PHP and MySQL, and want to calculate date time difference between two datetimes. I have a message table, in that table createdate is one field. I want to find out day and time difference from current date in the format 1 day 2 hours ago. What is the best way to go about this?

推荐答案

使用PHP的内置日期函数:

Use PHP's built in date functions:

<?php
    $start_time = "Y-m-d H:i:s"; // fill this in with actual time in this format
    $end_time = "Y-m-d H:i:s"; // fill this in with actual time in this format

    // both of the above formats are the same as what MySQL stores its
    // DATETIMEs in

    $start = new DateTime($start_time);
    $interval = $start->diff(new DateTime($end_time));

    echo $interval->format("d \d\a\y\s h \h\o\u\r\s");




  • DateInterval文档

  • DateTime :: diff文档

    • DateInterval documentation
    • DateTime::diff documentation
    • 这篇关于计算两个数据时间之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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