zecaluis
zecaluis

Reputation: 155

Condition LIKE with strtotime in codeigniter

Is it possible to create a condition for it to search values by day, month and year when the date was added with strtotime in the database?

The insert is done this way in the DB:

$data['date_added'] = strtotime(date('D, d-M-Y'));

The code I'm trying to make work:

$total = $this->db
        ->select_sum('amount','total')
        ->like('date_added', strtotime(date('01-02-2019')))
        ->get('payment')
        ->row_array();
        echo $total['total'];

If I enter the full date strtotime(date('01-02-2019')) it returns the results, but I need to search by month and year strtotime(date('02-2019')) or strtotime(date('2019')).

Is it possible to do that?

Upvotes: 0

Views: 239

Answers (1)

Michael Krikorev
Michael Krikorev

Reputation: 2156

You can convert the timestamp and compare it to your month-year string. Not tested but something like this:

->where('FROM_UNIXTIME(date_added, "%m-%Y") = "02-2019"', NULL, FALSE)

And for year:

->where('FROM_UNIXTIME(date_added, "%Y") = "2019"', NULL, FALSE)

Upvotes: 1

Related Questions