【www.gdgbn.com--mysql教程】

mysql教程 unix_timestamp()详解

若无参数调用,则返回一个 unix timestamp ("1970-01-01 00:00:00" gmt 之后的秒数) 作为无符号整数。若用date 来调用 unix_timestamp(),它会将参数值以"1970-01-01 00:00:00" gmt后的秒数的形式返回。date 可以是一个 date 字符串、一个 datetime字符串、一个 timestamp或一个当地时间的yymmdd 或yyymmdd格式的数字。
mysql> select unix_timestamp();
-> 882226357
mysql> select unix_timestamp("1997-10-04 22:23:00");
-> 875996580
当 unix_timestamp 被用在 timestamp列时, 函数直接返回内部时戳值, 而不进行任何隐含的 “string-to-unix-timestamp”转化。假如你向unix_timestamp()传递一个溢出日期,它会返回 0,但请注意只有基本范围检查会被履行 (年份从1970 到 2037, 月份从01 到12,日期从 01 到31)。

select `start_time` , `end_time` , unix_timestamp( now( ) )
from `fair_exhibition_info`
limit 0 , 30

start_time int开始时间            end_time int结束时间                unix_timestamp( now( ) )
1249314320                                  1251560723                                    1253244650
1251820776                                 1253721578                                    1253244650
1251827113                                  1253900715    

通过 unix_timestamp 函数把 mysql 数据库教程中的 date 类型数据转换成 unix timestamp 形式的一个整形数字:

select unix_timestamp("2006-02-28") testdate;

按理说得到的时间戳应该可以直接拿来给 php教程 的 date() 等函数使用。但奇怪的是:

echo date("y-m-d",$testdate);

显示出来的日期跟数据库实际的日期相比却少了一天,

把这八个小时加回去(unix_timestamp("2006-02-28" + interval 8 hour));或者弃用 unix_timestamp 函数, 直接得到 mysql date 字符串之后通过 strtotime() 函数来把字符串转化成真正的本地时间戳。

本文来源:http://www.gdgbn.com/shujuku/29969/