[英]php date and mysql timestamp comparison
How can I do a select from a db field that uses a timestamp when all I have from php is a date? 当我从php获取的所有内容都是日期时,如何从使用时间戳的db字段中进行选择?
$date = 2012-10-03;
something like: 就像是:
"select value from table where completed_date = $date"
only the value for completed_date is really something like "2012-10-03 02:16:10" what I really would like is all values that are the same day as $date and for the query to almost disregard the time aspect of the timestamp for this query. 只有completed_date的值真的像“2012-10-03 02:16:10”,我真正想要的是所有与$ date同日的值,并且查询几乎忽略了时间戳的时间方面对于此查询。 Is that possible?
那可能吗?
Thanks! 谢谢!
你需要尝试这样的事情,
"select value from table where completed_date like '$date%'"
鉴于您提供的日期已经是一个有效的mysql日期戳,它可以很简单:
SELECT ... WHERE DATE(completed_date)='2012-10-03'
使用DATE_FORMAT格式化mysql日期:
"select value from table where DATE_FORMAT(completed_date,'%Y-%m-%d') = $date"
If your database field is a Unix timestamp, you would use FROM_UNIXTIME
detailed here . 如果你的数据库字段是一个Unix时间戳,你会用
FROM_UNIXTIME
详细这里 。
If your databse field ( col
) is of datetime
type, you would SELECT ... WHERE col LIKE '2012-10-03%'
. 如果您的数据库字段(
col
)是datetime
类型,那么您将SELECT ... WHERE col LIKE '2012-10-03%'
。
If your database field ( col
) is of date
type, you would SELECT ... WHERE col = '2012-10-03'
. 如果您的数据库字段(
col
)是date
类型,那么您将SELECT ... WHERE col = '2012-10-03'
。
If completed_date
is a timestamp field, not varchar, then you could do something like this: 如果
completed_date
是时间戳字段,而不是varchar,那么你可以这样做:
"SELECT value FROM table WHERE completed_date>=? AND completed_date<?"
And bind in the parameters: 并绑定参数:
$date="2012-10-03";
$start=date("Y-m-d G:i:s",strtotime($date));
$end =date("Y-m-d G:i:s",strtotime($date.' +1day'));
I like coder1984's answer as one that doesn't require a schema change. 我喜欢coder1984的答案,因为它不需要架构更改。 I would however suggest just adding a field in your database that is a date field if this is how you are typically going to query the data.
但是,我建议只在数据库中添加一个日期字段,如果这是您通常要查询数据的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.