【问题标题】:Find the difference between current time and time value stored in the database?找出存储在数据库中的当前日期和时间值之间的差异?
【发布时间】:2014-05-19 13:21:34
【问题描述】:
$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'); 一样直接给出时间时可以计算差异,但是当我试图获取存储在数据库中的时间值时,我不能!!!

快来救救我!!!我正在尝试很多......

【问题讨论】:

  • 你在db中存储了什么类型的数据
  • 我已将其存储为 Varchar。
  • 对不起,这不起作用
  • 为什么不能更改 db 以在单个字段中容纳日期时间。那么你可以这样做stackoverflow.com/questions/2832467/…
  • 我试过了,但是当我将日期和时间存储在同一个字段中时,在我获取它并使用 srttotime() 进行转换后,差异会在几分钟内变成这样的 2348900……不完全是。但差异仅显示为这样......这就是为什么它试图在单独的字段中更改它......

标签: php


【解决方案1】:

试试这个你需要专门设置时区否则默认需要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";

【讨论】:

  • "date_default_timezone_set("Asia/Kolkata");"---> 添加此代码后,我得到了区别。非常感谢!!
猜你喜欢
  • 1970-01-01
  • 2015-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
  • 2023-02-02
  • 2011-12-10
  • 1970-01-01
相关资源
最近更新 更多