下面给出的Mysql列send_date日期时间格式
1423305800
1423305866
1423305932
1423305998
1423306064
如何从这张表中只选择日期,我的编码是:
$date_from_calendar = strtotime('2015-02-06');
$sql = "select * from use_info where user_id='$user_id' and date(send_date)= '$date_from_calendar' order by u_id DESC";
为什么我无法从中获得结果?
答案 0 :(得分:1)
怎么样,在比较之前格式化
$date_from_calendar = strtotime('2015-02-06');
$sql = "select * from use_info where user_id='$user_id' and unix_timestamp(from_unixtime(send_date, '%Y-%m-%d')) = '$date_from_calendar' order by u_id DESC";
答案 1 :(得分:0)
在php strtotime()函数中,会将字符串日期(' 2015-02-06')转换为您在数据库中使用的时间戳,因此您不需要使用查询中的date()函数,这里是更正后的代码:
$thatday = '2015-02-06';
$sql = "select * from use_info where user_id='$user_id' and date(send_date)= '$thatday' order by u_id DESC";
答案 2 :(得分:0)
如果send_date
字段的类型为timestamp,那么您可以将其转换为日期。
$sql = "select * from use_info
where user_id='$user_id'
and date(send_date) = '2015-02-06'
order by u_id DESC";
在问题中,您将时间戳转换为日期并将其与时间戳进行比较。
或者你可以手动"搜索一整天。
date_default_timezone_set('UTC');
$start = strtotime('2015-02-06'); // start of the day 1423180800
$end = $start + (60 * 60 * 24); // 24 hours after $start 1423267200
$sql = "select * from use_info
where user_id = $user_id
and send_date >= $start
and send_date < $end
order by u_id DESC";
在上面的示例中,我们匹配1423180800
之后或1423267200
之前或之后发生的所有记录。