简体   繁体   English

MySQL日期格式-日期查询困难

[英]MySQL date formats - difficulty querying on a date

I want to perform a simple query like this in my PHP, 我想在我的PHP中执行一个简单的查询,

SELECT noslots FROM calender WHERE coursedate = '01-01-2012';

however my dates are stored in the database in the standard YYYY-MM-DD format so obviously I get no reults. 但是我的日期以标准的YYYY-MM-DD格式存储在数据库中,因此显然我没有任何结果。

I have tried reading through the manual but for a beginner it is difficult to extract the correct information 我已经尝试通读手册,但是对于初学者来说,很难提取正确的信息

thank you for any help you can offer! 感谢您提供的任何帮助!

[= [=

You can use - 您可以使用 -

SELECT noslots
FROM calender
WHERE coursedate = STR_TO_DATE('01-01-2012', '%d-%m-%Y');

You might need to switch the %d and %m around depending on your date format. 您可能需要根据日期格式切换%d和%m。

MySQL's ONLY supported date/time format is yyyy-mm-dd hh:mm:ss . MySQL唯一支持的日期/时间格式为yyyy-mm-dd hh:mm:ss You cannot pass any other string and have it properly parsed as a date. 您不能传递任何其他字符串并将其正确解析为日期。 It is up to you to properly format your dates into the mysql format, meaning you'll have to pass 2012-01-01 in the query. 您可以自行决定将日期正确设置为mysql格式,这意味着您必须在查询中传递2012-01-01

我将使用PHP的date()strtotime()函数:

mysql_query("SELECT `noslots` FROM `calender` WHERE `coursedate` = '" . date('Y-m-d H:i:s', strtotime('01-01-2012')) . "'");

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

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