gpt4 book ai didi

php - PHP根据时间获取正确的数据库记录

转载 作者:行者123 更新时间:2023-11-29 17:29:05 25 4
gpt4 key购买 nike

请尝试使用单选按钮从数据库中获取我的任务,但是我没有获得正确的记录,我使用了Codeigniter

这是我的代码:

switch(strtolower($this->input->post('tasksfilter'))){
case "overdue":
$query = array("task_accomplish_date <=" => time());
break;
case "today":
$query = array("task_accomplish_date <=" => strtotime("today midnight"));
break;
case "tomorrow":
$query = array("task_accomplish_date <=" => strtotime("tomorrow midnight"));
break;
}
$this->data['result'] = $this->crud->read_where("tasks", $query)->result();


这也是我的 read_where函数:

public function read_where($table, $cond, $limit = null, $offset = null){
return $this->db->get_where($table, $cond, $limit, $offset);
}


NB * task_accomplish_date是包含 varchar值的 timestamp字段
我在这里先向您的帮助表示感谢

最佳答案

问题在于您总是在寻找小于或等于特定日期的日期。因为“今天”总是小于“明天”,所以您的答案自然也会得到“今天”。

如果task_accomplish_date列中的值始终是所讨论的当天的午夜,则可以使用相等运算符。如果使用除午夜以外的某些“时间”,那么我将不得不修改答案。以下假设该值始终为午夜。

此答案还假设您只希望“明天”,而不希望以后的任何天,即后天。

$findDay = strtotime("today");
switch (strtolower($this->input->post('tasksfilter')))
{
case "overdue":
$findDay -= 86400; //subtract one days worth of seconds
break;
case "tomorrow":
$findDay += 86400; //add a day to "today"
break;
}
$query = array("task_accomplish_date == " => $findDay);
$this->data['result'] = $this->crud->read_where("tasks", $query)->result();

关于php - PHP根据时间获取正确的数据库记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50789218/

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