简体   繁体   English

如何从包含unix时间戳中的日期字段的表中获取记录?

[英]How to get record from table which contain date field in unix timestamp?

I am storing my date by using the time() function in PHP. 我在PHP中使用time()函数存储我的日期。 I have a search section in my program.And the field for date is done by jquery datepicker. 我的程序中有一个搜索部分。日期字段由jquery datepicker完成。 This is my date format eg: 14/12/2012. 这是我的日期格式,例如:14/12/2012。

The problem is the filed in table is timestamp. 问题是表中的字段是时间戳。 How can i select the actual date from this date string using MySQL query? 如何使用MySQL查询从此日期字符串中选择实际日期?

I am using codeigniter as framework. 我使用codeigniter作为框架。 Is there any function to do this? 这有什么功能吗?

Please help me. 请帮我。

time() in PHP simply returns a unix time stamp - seconds since Jan 1/1970. PHP中的time()只返回一个unix时间戳 - 自1970年1月1日以来的秒数。 MySQL timestamp, date, and datetime fields are NOT directly compatible with that value, but you can convert between the two easily: MySQL时间戳,日期和日期时间字段与该值不直接兼容,但您可以轻松地在两者之间进行转换:

SELECT UNIX_TIMESTAMP(yourtimestampfield) AS php_compatible_time_value, ...

Note that MySQL's date string format is yyyy-mm-dd . 请注意,MySQL的日期字符串格式为yyyy-mm-dd You cannot use your dd/mm/yyyy format unless you convert to MySQL's expected format. 除非转换为MySQL的预期格式,否则不能使用dd/mm/yyyy格式。

$date = '14/12/2012';
$d = explode('/',$date);
$timestamp = mktime(0, 0, 0, $d[1], $d[0], $d[2]); 

And then you use $timestamp in your query 然后在查询中使用$ timestamp

Of course you're gonna need different approaches for dates in different format UK/US etc. mm/dd/yyyy or dd/mm/yyyy 当然,你需要采用不同格式的英国/美国等日期不同的方法mm / dd / yyyy或dd / mm / yyyy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM