duanhuokuang5280 2010-01-05 14:32 采纳率: 100%
浏览 117
已采纳

PHP mySQL - UNIX_TIMESTAMP不等于strtotime

PLATFORM: PHP & mySQL

I am storing the date+time in database in the following format: date("Y-m-d H:i:s"). An example of a value that I have in my DB is : 2010-01-05 07:36:33. In my script, I have set the timezone as date_default_timezone_set("America/Chicago");

THE PROBLEM:

I read about the UNIX_TIMESTAMP somewhere and I was using that in my query. The value of UNIX_TIMESTAMP on a date value from the DB, seems to be different from the strotime(DB date Value).

EXAMPLE:

Consider that one of the DB values for the date column in my DB is 2010-01-05 07:36:33 Now if I fetch this date in the following way:

$result = mysql_query("SELECT date, UNIX_TIMESTAMP(date) AS ut_date FROM table");
$row = mysql_fetch_row($result);

//The result of this is:
$row['date']    = 2010-01-05 07:36:33
$row['ut_date'] = 1262657193
strtotime($row['date']) gives 1262698593

For my further calculations within my application, I need to work with strtotime(date). I have many comparisons to do that way. My problem would have solved, had the UNIX_TIMESTAMP(date) was same as strtotime(date). One of the sample query that I need to work with, is:

$gap = 1; // time in minutes
$tm2 = date ("Y-m-d H:i:s", mktime (date("H"),date("i")-$gap,date("s"),date("m"),date("d"),date("Y")));
$target = strtotime($tm2);

$result2 = mysql_query("UPDATE table2 SET stat = 0 WHERE UNIX_TIMESTAMP(today_login_time) < $target ");

The above is giving me incorrect results. If I try to replace UNIX_TIMESTAMP with strtotime in the above query, it gives me an error as the function strtotime seems to be PHP function and not respective mySQL function. Is there a respective mySQL function for the strtotime ? How do I solve the above problem? The code to solve the above problem is highly appreciated.

Thank you in advance.

  • 写回答

4条回答 默认 最新

  • dpd66100 2010-01-05 14:40
    关注

    Probably the timezones of your MySQL server and your php instance differ. e.g.

    echo date('Y-m-d H:i:s T', 1262657193);

    prints on my machine 2010-01-05 03:06:33 CET (note the CET timezone, that's UTC+1) while your MySQL server interprets the same unix timestamp as 2010-01-05 07:36:33

    see http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html
    and http://docs.php.net/datetime.configuration#ini.date.timezone

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记