Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

mysql - Return the difference between two (dates and time ) PHP

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

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");

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.

I am running PHP Version 5.3.3

Edit: 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 :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Try this
function getTimes($t1, $t2)
{
$timeFirst  = strtotime($t1);
$timeSecond = strtotime($t2);
$differenceInSeconds = $timeSecond - $timeFirst;
$h=0;
$m  = floor($differenceInSeconds / 60);
$s  = $differenceInSeconds % 60;
if ($m>=60)
{
  $h = floor($m / 60);
  $m = $m % 60;
}
$tim = $h.':'.$m.':'.$s;
return $tim;
}
it will return difference time in hours:min:sec

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...