本文介绍了返回两个(日期和时间)PHP之间的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了堆栈溢出时可用的大部分功能,但似乎没有任何作用.

i tried most of what is available on stack overflow but none seem to work.

我尝试比较两种方式(日期和时间格式).并计算它们之间的差异是否在5秒钟之内

any way i am trying to compare two (date and time formats). and calculate whether their difference is within 5 seconds of each other

第一个日期是当前日期:

the first date is the current date:

$today   = date("Y-m-d H:i:s");

第二个日期取自mysql数据库:

the second date is taken from mysql database:

$result = mysql_query("SELECT * FROM BUS_DATA where BusRegID = 'bus'") or die(mysql_error());
$row     = mysql_fetch_array($result);
$update_date  = $row["update_date"];

答案应分为年,月,日,小时,分钟和秒.

the answer should be segmented into years , month , days , hours , minutes ,and seconds portions.

我正在运行PHP版本5.3.3

I am running PHP Version 5.3.3

大多数答案只给出时间范围内的结果,我想检查日期是否匹配,然后比较一天中的时间是否在5秒以内,所以请比你们提前:)

most answers give result in time frame only , I want to check whether the date matches , then compare if the time of the day is within 5 seconds , than you guys in advance :)

推荐答案

使用strtotime,它几乎可以理解任何日期格式:

use strtotime, it's understands almost any date formats:

$timeDiff = abs(strtotime($today) - strtotime($update_date));

这为您提供了以秒为单位的日期之间的时差.

this gives you the difference between dates in seconds.

这篇关于返回两个(日期和时间)PHP之间的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 06:26