gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:06:54 25 4
gpt4 key购买 nike

我正在使用以下代码构建一个 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.

默认参数是 0 即星期日,将其设置为 1 将是星期一、2 星期二等。

week(date,1);

关于php - 在 PHP/MySQL 中使用 Week(Date) 时,如何将一周的第一天设置为星期一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16196708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com