找出存储在数据库中的当前日期和时间值之间的差异?

时间:2014-05-17 12:22:23

标签: php

$fetch=mysql_query("select * from code where user_email='$email' order by reg_date desc limit 1");
$db=mysql_fetch_array($fetch);
print_r($db);
$db_date=$db['reg_date'];
$db_time=$db['time'];
echo "DB_Date:".$db_date."--";
echo "DB_time:".$db_time."--";


        $current_date = date('Y-m-d');  
        echo "present date".$current_date."--";
        $curent_time = date('H:i:s');
        echo "current time:".$curent_time."--";
        $first_time = strtotime($curent_time);
        $last_time = strtotime($db_time);
        $time = $first_time - $last_time;

        $time_differenc= ($time/60)%60;
        echo "difference".$time_difference."--";

在上面的代码中,我正确地获取存储在数据库中的当前时间,日期和时间,日期值,但是当我试图找到存储在数据库中的当前时间和时间之间的差异时,我无法得到差。

当我像strtotime一样直接给出时间时('11:30:02');差异可以计算,但当我试图获取存储在数据库中的时间值时,我不能!!!

尽快帮助我!!!我正在努力......

1 个答案:

答案 0 :(得分:2)

试试这个你需要专门设置时区,默认情况下需要UTC

$dbdatetime = $row['datetime'];//datetime from database: "2014-05-18 18:10:18"
date_default_timezone_set("Asia/Kolkata"); //setting default timezone based on your location
$curdatetime = date("Y-m-d H:i:s"); //current datetime

if($curdatetime > $dbdatetime){
    $diff = abs(strtotime($curdatetime) - strtotime($dbdatetime));
}else{
    $diff = abs(strtotime($dbdatetime) - strtotime($curdatetime));
}
echo "The difference is "$diff/60." minutes";