【问题标题】:Getting a certain day from the current week then Query that date从当前周获取某一天,然后查询该日期
【发布时间】:2020-07-08 16:58:25
【问题描述】:

我正在尝试从本周获取周一至周五的日期。然后使用这些日期来查询我的 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' ) );

两种方式都不显示任何结果。

【问题讨论】:

  • 尝试检查你的 SQL

标签: php mysql sql database


【解决方案1】:

你也可以使用 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

【讨论】:

  • 这行得通,只是它显示了星期一的所有工作。我只想显示本周的星期一。
【解决方案2】:

我找到了解决办法。

全部在 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';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-08
    • 2011-04-14
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    相关资源
    最近更新 更多