在PHP中使用SQL查询获取当前(系统)日

时间:2012-01-18 13:15:07

标签: php mysql

我有这个MySQl数据库有一个表,其中包含一个包含星期几的列,如“Mon”,“Tue”等。

如何查询数据库并将此列内容与当前(系统)日匹配?

喜欢说

select .... where tablename.columnname = systemday

感谢。

5 个答案:

答案 0 :(得分:3)

尝试使用date_format(now(), "%a")作为条件值。

在此处阅读更多内容:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

答案 1 :(得分:1)

Haven你试过搜索吗?

无论如何,我希望你想要这样的东西,

$ cur_day = date(' D');

select .... where tablename.columnname = $cur_day;

答案 2 :(得分:0)

WHERE tablename.columnname = substr(dayname(now()),1,3)

WHERE tablename.columnname = date_format(now(),'%a')

但请注意,它可能会受到区域设置的影响。

答案 3 :(得分:0)

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_weekday

Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_dayofweek

Returns the weekday index for date (1 = Sunday, 2 = Monday, …, 7 = Saturday). These index values correspond to the ODBC standard.

示例:

SELECT * FROM mytable WHERE dayOfTheWeek=WEEKDAY(NOW())

此外,您可以使用PHP的strftimedate函数执行此操作:

$query1 = mysqli_query('SELECT * FROM mytable WHERE dayOfTheWeek="'.date('N').'"');
$query2 = mysqli_query('SELECT * FROM mytable WHERE dayOfTheWeek="'.strftime('%u').'"');

但是,我认为,最好以仅PHP的方式生成它。

答案 4 :(得分:-3)

<?php 
$day=date('D');

$sql='select * from the_table where the_table.'.$day.'=systemday';
?>