Richa Sharma
Richa Sharma

Reputation: 41

How to check day is between to days in mysql

I have two fields in mysql like following,

Id from_day to_day
1   2       6
2   1       4
3   4       5

from_day and to_day is current day like Mon is 1 and Sun is 7.

Now I want to check if id 2 is in current day like today is Wed(3). How I can check with mysql query.

Upvotes: 1

Views: 68

Answers (2)

Ullas
Ullas

Reputation: 11556

You can check whether DAYOFWEEK(CURDATE()) is between from_day and to_day column values using a CASE expression.

Query

select 
  case when dayofweek(curdate()) between `from_day` and `to_day` then 'yes' 
  else 'no' end as `status`
from `your_table_name`;

Upvotes: 1

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

You can try this:

$day_number = date('N');  // here $day_number returns the day 3 for today's date

and use mysql between to check whether it lies between from_day and two_day like:

"SELECT * FROM TABLE WHERE ".$day_number ." BETWEEN from_day AND two_day";

Upvotes: 0

Related Questions