Md Abdul Hakim
Md Abdul Hakim

Reputation: 1

Collect data from last 5 days in MySQL

I have following code

    $date = date("Y-m-d");

    public function ip2_exists() {

        global $db,$date;

        $code = $db->select("imp","id",array("date"=>$date,"ip"=>$this->ip()));
        if($code->num_rows<1){
            return false;
        }
        else {
            return true;
        }
    }

It collects the current day data and it works fine. But I want to collect the last 5 days date.

How to do that in php?

Upvotes: 0

Views: 54

Answers (1)

user8735467239
user8735467239

Reputation: 386

Try:

$date = date("Y-m-d");

public function ip2_exists()
{

    global $db,$date;

    // Used to keep count track
    $countDays = 0;

    // Default return of true
    $checkedData = true;

    while($countDays < 5)
    {
        $code = $db->select("imp","id",array("date"=>$date,"ip"=>$this->ip()));
        if($code->num_rows < 1)
        {
            $checkedData = false;
        }

        $countDays++;
        $date = date("Y-m-d", strtotime( $date." -1 day"));
    }

    return $checkedData;
}

Upvotes: 2

Related Questions