数据库
首页 > 数据库> > 在PHP / MySQL中使用Week(Date)时,如何将一周的第一天设置为星期一?

在PHP / MySQL中使用Week(Date)时,如何将一周的第一天设置为星期一?

作者:互联网

我正在使用以下代码构建Google图表,以提取与一年中的周数相对应的MySQL表中的所有条目.目前,周号从星期日开始,我想更改此值,以便它们从星期一开始,但是我不确定如何执行此操作.

$i=1;
    while($i<=53)
       {
        $week_start = new DateTime();
        $week_start->setISODate(2013,$i+1);
        $date_display = $week_start->format('j M Y');

        $sessions = $wpdb->get_var($wpdb->prepare("SELECT Sum(Due) from patient_sessions WHERE Type='Session' AND Week(Date)='$i'"));

        $temp = array();

        $temp[] = array('v' => (string) $date_display); 
        $temp[] = array('v' => (string) $sessions); 
        $rows[] = array('c' => $temp);
        $i++;
    }

修改后的代码

$sessions = $wpdb->get_results($wpdb->prepare("SELECT Due,Date from patient_sessions WHERE Type='Session'"));
$work_times = $wpdb->get_results($wpdb->prepare("SELECT Amount,Date from work_times"));
$expenses = $wpdb->get_results($wpdb->prepare("SELECT Amount,Date from expenses WHERE Client='Psychotherapy'"));

$i=1;
while($i<=53) {
    $week_start = new DateTime();
    $week_start->setISODate(2013,$i+1);
    $date_display = $week_start->format('j M Y');

    $session_total = 0;
    $work_time_total = 0;
    $expense_total = 0;


      foreach ($sessions as $session) {
    if (date("W", strtotime($session->Date)) == $i) {
    $session_total = ($session_total+$session->Due);
    }
    }

         foreach ($work_times as $work_time) {
    if (date("W", strtotime($work_time->Date)) == $i) {
    $work_time_total = ($work_time_total+$work_time->Amount);
    }
    }

          foreach ($expenses as $expense) {
    if (date("W", strtotime($expense->Date)) == $i) {
    $expense_total = ($expense_total+$expense->Amount);
    }
          }

    $balance = ($session_total + $work_time_total - $expense_total);

    $temp = array();

    $temp[] = array('v' => (string) $date_display); 
    $temp[] = array('v' => (string) $balance); 
    $rows[] = array('c' => $temp);
    $i++;
}

解决方法:

WEEK(date[mode]); 

date = a date value.    
mode = An integer indicating the starting of the week. 

默认arugment为0,即星期日,将其设置为1将是星期一,2星期二,依此类推.

week(date,1);

标签:php,mysql,date,week-number
来源: https://codeday.me/bug/20191012/1897439.html