Select records from today, this week, this month php mysql

前端 未结 11 867
粉色の甜心
粉色の甜心 2020-12-12 10:50

I imagine this is pretty simple, but can\'t figure it out. I\'m trying to make a few pages - one which will contain results selected from my mysql db\'s table for today, thi

11条回答
  •  猫巷女王i
    2020-12-12 11:10

    I think using NOW() function is incorrect for getting time difference. Because by NOW() function every time your are calculating the past 24 hours. You must use CURDATE() instead.

        function your_function($time, $your_date) {
        if ($time == 'today') {
            $timeSQL = ' Date($your_date)= CURDATE()';
        }
        if ($time == 'week') {
            $timeSQL = ' YEARWEEK($your_date)= YEARWEEK(CURDATE())';
        }
        if ($time == 'month') {
            $timeSQL = ' Year($your_date)=Year(CURDATE()) AND Month(`your_date`)= Month(CURDATE())';
        }
    
        $Sql = "SELECT * FROM jokes WHERE ".$timeSQL
        return $Result = $this->db->query($Sql)->result_array();
    }
    

提交回复
热议问题