如何重置周日晚上11:59(周一开始前的时间)从数据库中提取的记录



我目前正在开发一个php CodeIgniter项目,需要在本周周一和本周周日之间获取数据并执行计数。我可以获取本周星期一和本周星期日的日期,以及计数,但在另一件事上面临问题;一旦日期发生变化,我需要在周日晚上重置计数,即周日晚上11:59 PM,因此一旦达到此时间,如果计数为40,则在周一上午12:00,计数应从0重新开始。下面是我写的函数:

$this_week_start = date("Y-m-d" , strtotime("monday this week"));
$this_week_end = date("Y-m-d" , strtotime("sunday this week"));
$table = $this->db->table('orders');
$table->select('*')->where("status_id = 16")->where("updated_at >= '$this_week_start'")->where("updated_at <= '$this_week_end'");
$data = $table->get()->getResultArray();
if ($data)
return $data;

在这方面的任何帮助都将不胜感激。

根据这个小测试,日期应该在周日午夜正确滚动,以显示新一周的日期。因此,错误只能出现在代码的其他地方

# setup a fixed timestamp for ...
$midnight_minus1 = 1639958399;  // saturday at 23:59:59
$midnight        = 1639958400;  // sunday at 00:00:00
$midnight_plus1  = 1639958401;  // sunday at 00:00:01
echo 'AT midnight minus 1 second ' . date('Y-m-d H:i:s', $midnight_minus1) . PHP_EOL;
$this_week_start = date("D - Y-m-d" , strtotime("monday this week", $midnight_minus1));
echo $this_week_start . PHP_EOL;
$this_week_end = date("D - Y-m-d" , strtotime("sunday this week", $midnight_minus1));
echo $this_week_end . PHP_EOL;
echo PHP_EOL . 'AT midnight precisely ' . date('Y-m-d H:i:s', $midnight) . PHP_EOL;
$this_week_start = date("D - Y-m-d" , strtotime("monday this week", $midnight));
echo $this_week_start . PHP_EOL;
$this_week_end = date("D - Y-m-d" , strtotime("sunday this week", $midnight));
echo $this_week_end . PHP_EOL;
echo PHP_EOL . 'AT midnight plus 1 second ' . date('Y-m-d H:i:s', $midnight_plus1) . PHP_EOL;
$this_week_start = date("D - Y-m-d" , strtotime("monday this week", $midnight_plus1));
echo $this_week_start . PHP_EOL;
$this_week_end = date("D - Y-m-d" , strtotime("sunday this week", $midnight_plus1));
echo $this_week_end . PHP_EOL;

结果

AT midnight minus 1 second 2021-12-19 23:59:59
Mon - 2021-12-13
Sun - 2021-12-19
AT midnight precisely 2021-12-20 00:00:00
Mon - 2021-12-20
Sun - 2021-12-26
AT midnight plus 1 second 2021-12-20 00:00:01
Mon - 2021-12-20
Sun - 2021-12-26

最新更新