简体   繁体   English

从当前周获取某一天,然后查询该日期

[英]Getting a certain day from the current week then Query that date

Im trying to get the dates from Monday - Friday from the current week.我试图从本周的星期一到星期五获取日期。 then use those dates to query my mysql database.然后使用这些日期查询我的 mysql 数据库。 But my current code is not working.但是我当前的代码不起作用。

$monday = date("Y-m-d",strtotime('next monday', strtotime('previous sunday')));
$sql = 
'SELECT *
FROM customers, jobs
WHERE customers.customerid = jobs.customerid AND duedate = "$monday" AND (jobs.status IS NULL OR jobs.status = "2" OR jobs.status = "3" OR jobs.status = "1" OR jobs.status = "5") AND (jobs.jobtype = "a" OR jobs.jobtype = "A" OR jobs.jobtype = "m" OR jobs.jobtype = "M")
ORDER BY duedate, jobtype';

I have Also tried use this code我也尝试过使用此代码

$monday = date( 'Y-m-d', strtotime( 'monday this week' ) );

Both ways it doesnt show any results.两种方式都没有显示任何结果。

You can also use dayoftweek()您也可以使用 dayoftweek()

for monday = 2星期一 = 2

    SELECT *
    FROM customers, jobs
    WHERE customers.customerid = jobs.customerid 
    AND DAYOFWEEK(duedate) =  2 
    AND (jobs.status IS NULL OR jobs.status = "2" 
        OR jobs.status = "3" 
        OR jobs.status = "1" 
        OR jobs.status = "5") 
    AND (jobs.jobtype = "a"
     OR jobs.jobtype = "A" 
     OR jobs.jobtype = "m" 
     OR jobs.jobtype = "M")
    ORDER BY duedate, jobtype

I found the solution.我找到了解决方案。

Did it all in the sql query.这一切都在 sql 查询中完成。

                        $sql = 
                        'SELECT *
                        FROM customers, jobs
                        WHERE customers.customerid = jobs.customerid AND DAYOFWEEK(duedate) =  3 AND YEARWEEK(duedate) = YEARWEEK(NOW())
                        AND (jobs.status IS NULL OR jobs.status = "2" OR jobs.status = "3" OR jobs.status = "1" OR jobs.status = "5") 
                        AND (jobs.jobtype = "a" OR jobs.jobtype = "A" OR jobs.jobtype = "m" OR jobs.jobtype = "M")
                        ORDER BY duedate, jobtype';

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

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