Robert Granlund
asked on
MySQL Query WHERE WEEK of
Instead of doing it by current date is there a way to do it by the week of? NOT week(CURRENT()) I want to do as the week of:
<?php
$bg = '#eeeeee';
$sql->query("SELECT doctor_doctor.doctor_id, doctor_doctor.first_name AS first_name, doctor_doctor.last_name AS last_name, doctor_schedule.doctor_id, doctor_schedule.schedule_date AS date, doctor_schedule.time AS time, doctor_schedule.location_id
FROM doctor_doctor, doctor_schedule
WHERE THE WEEK OF 2013--01-01 AND doctor_doctor.doctor='1' AND doctor_doctor.doctor_id=doctor_schedule.doctor_id AND doctor_schedule.location_id='1'
ORDER BY date DESC");
$row = $sql->fetch_all();
// var_dump($row);
foreach($row as $r){
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
echo '<tr bgcolor="' . $bg . '">';
echo '<td height="25" width="150">'.$r['first_name'].' '.$r['last_name'].'</td><td height="25" width="150"> '.$r['date'].'</td><td height="25" width="150"> '.$r['time'].'</td>';
echo '</tr>';
}
?>
Hi rgranlund,
You can create a stored function inside your database, able to compute if a given date belongs to the week of a reference day:
Then call your function from your SQL (here we assume testing if the date value of field my_date_field is in the current week, the week the current day belongs to)
or, if you don't want to use stored function, just use the where condition
You can create a stored function inside your database, able to compute if a given date belongs to the week of a reference day:
CREATE FUNCTION `date_is_in_week_of`(`test_date` date, `ref_date` date) RETURNS tinyint(1)
BEGIN
RETURN test_date BETWEEN date_sub(ref_date, interval weekday(ref_date) day)
AND date_add(ref_date, interval 6-weekday(ref_date) day);
END
Then call your function from your SQL (here we assume testing if the date value of field my_date_field is in the current week, the week the current day belongs to)
SELECT Field1, Field2
FROM my_table
WHERE date_is_in_week_of(my_date_field, curdate()) = 1
or, if you don't want to use stored function, just use the where condition
SELECT Field1, Field2
FROM my_table
WHERE my_date_field BETWEEN date_sub(curdate(), interval weekday(curdate()) day)
AND date_add(curdate(), interval 6-weekday(curdate()) day)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hope you take the time to read the article! Good luck with your project, ~Ray
Ray's articles are always a good read :)
Thanks for the grading. Cheers, Paul
Thanks for the grading. Cheers, Paul
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html