San*_*tri 8 php mysql datetime
我正在使用PHP和MySQL,并希望计算两个日期时间之间的日期时间差.我有一个消息表,在该表中createdate是一个字段.我想从格式中找出当前日期的日期和时间差异1 day 2 hours ago.最好的方法是什么?
使用PHP的内置日期函数:
<?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");
Run Code Online (Sandbox Code Playgroud)
SELECT TIMESTAMPDIFF(HOUR,createdate,NOW()) as diff_in_hours FROM table1;
Run Code Online (Sandbox Code Playgroud)
然后在 php 端,您可以轻松地将 的值转换diff_in_hours为天 + 小时格式。
| 归档时间: |
|
| 查看次数: |
9398 次 |
| 最近记录: |