【问题标题】:How to reset record fetched from database at 11:59pm of Sunday (time before Monday starts)如何重置周日晚上 11:59(周一开始前的时间)从数据库中获取的记录
【发布时间】:2022-01-18 18:58:18
【问题描述】:

我目前正在开发一个 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;

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

【问题讨论】:

  • 当然是自动的。您的查询将在午夜重置开始和结束时间,您将假设返回 NO Rows,因此计数将为 0
  • @Andy 我想你完全误解了我,这里提供的代码是完全相关的。此外,我在这里没有提到对我有用的地方,最后我明确提到“这方面的任何帮助”。就您而言,代码不会对数据库进行任何更新,我还提到我正在获取数据计数。
  • “重置记录”是什么意思?您发布的代码从orders 表中获取所有数据,其中status_id = 16updated_at 时间介于“本周星期一”和“本周星期日”之间。当您使用-&gt;getResultsArray() 获取该数据时,它只会为您提供数据库中与这些条件匹配的任何内容。生成的数据是动态的,因此无需“重新启动”任何东西。在任何给定时间点重新运行脚本都会产生动态数据。如果返回的数据是数组格式,PHP 索引从 0 开始,所以不需要“重启”任何东西。
  • @RiggsFolly 是的,但是这段代码不会在午夜返回零计数,而是在清晨的某个地方重置。

标签: php mysql codeigniter codeigniter-4


【解决方案1】:

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

# 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

【讨论】:

    猜你喜欢
    • 2019-10-11
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多