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

ctehm74n  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(332)

我想把星期一到星期五的日期从本周算起。然后用这些日期来查询我的mysql数据库。但是我现在的代码不起作用。

$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';

我也试过使用这个代码

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

两种方法都没有显示任何结果。

fzwojiic

fzwojiic1#

也可以使用dayoftweek()
星期一=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
eeq64g8w

eeq64g8w2#

我找到了解决办法。
在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';

相关问题