简体   繁体   中英

getting current (system) day using an SQL query in PHP

I have this MySQl database that has a table which contains a column that contains days of the week like "Mon", "Tue" etc.

How do I get query the database and match this column content with the current (system) day?

like say

select .... where tablename.columnname = systemday

thanks.

Try using date_format(now(), "%a") as your condition value.

Read more here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

Haven't you tried searching ?

Anyways i hope you want something like this,

$cur_day= date('D');

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

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

or

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

Note, however, that it may be affected by locale settings.

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.

Example:

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

Also, you can do this using PHP`s strftime and date functions:

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

But, I think, it is better to generate this in PHP-only way.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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