Nirali Joshi Nirali Joshi - 1 year ago 118
PHP Question

date comparision with laravel 4.2

I want to fetch records which are older than 2 days.

My current database records for

field is just one row dated
2016-05-01 12:23:23

I used below code

$date = new DateTime;
$date->modify('-2 days');

$formatted_date = $date->format('Y-m-d H:i:s');
echo $formatted_date; // prints 2016-04-29 14:27:42

$result = DB::table('cart_reminder')->whereDate('added_to_cart_at','<=',$formatted_date)->get();
foreach ($result as $row)
echo $row->user_email;

Ideally above
condition should fail and should return 0 records. But it fetches a record.

$result = DB::table('cart_reminder')->whereDate(\Carbon\Carbon::now()->subDays(2)->toDateString())->get();
foreach ($result as $row)
echo $row->user_email;

it prints error

production.ERROR: exception 'ErrorException' with message 'Missing argument 2 for Illuminate\Database\Query\Builder::whereDate(), called in


Answer Source

You're not specifying a column.

whereDate('added_to_cart_at', $carbon)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download