最近更新的BLOG列表
最新更新的文章列表
笔记:PHP中日期比较摘要
2008-01-11 15:20:49.0
目的:
用SQL语句查询MySQL数据库中某一个时间段里的记录.
数据库部分字段列表:
数据表:v_reports
字段:F_reportSubmitDate Date
F_reportStartDate Date
用到的知识点:
1、PHP和MYSQL中日期字符串转无符号时间整数(即1970.1.1 00:00:00至今的秒数).
实现过程摘要:
用SQL语句查询MySQL数据库中某一个时间段里的记录.
数据库部分字段列表:
数据表:v_reports
字段:F_reportSubmitDate Date
F_reportStartDate Date
用到的知识点:
1、PHP和MYSQL中日期字符串转无符号时间整数(即1970.1.1 00:00:00至今的秒数).
实现过程摘要:
1、获取需要查询的起止时间字符串.
$tempStartDate=mktime(0,0,0,1,7,2007)-28800;
$tempSubmitDate=mktime(0,0,0,1,11,2007)-28800; //此处减去28800是由于这里的MYSQL服务器的时区与PHP使用的不同,PHP用的是UTC+8.mktime()得到的是无符号的时间整数.
$tempStartDate=mktime(0,0,0,1,7,2007)-28800;
$tempSubmitDate=mktime(0,0,0,1,11,2007)-28800; //此处减去28800是由于这里的MYSQL服务器的时区与PHP使用的不同,PHP用的是UTC+8.mktime()得到的是无符号的时间整数.
2、构建SQL语句.
$sql="select * from v_reports where ((UNIX_TIMESTAMP(F_reportSubmitDate)<=$tempSubmitDate) AND (UNIX_TIMESTAMP(F_reportStartDate)>=$tempStartDate))"; //UNIX_TIMESTAMP()是MYSQL函数,和mktime()一样得到的是无符号的时间整数.
$sql="select * from v_reports where ((UNIX_TIMESTAMP(F_reportSubmitDate)<=$tempSubmitDate) AND (UNIX_TIMESTAMP(F_reportStartDate)>=$tempStartDate))"; //UNIX_TIMESTAMP()是MYSQL函数,和mktime()一样得到的是无符号的时间整数.
文章评论
[以下网友留言只代表其个人观点,不代表中华网的观点或立场]