Nirali Joshi Nirali Joshi - 7 months ago 30
PHP Question

date comparision with laravel 4.2

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

My current database records for

added_to_cart_at
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
where
condition should fail and should return 0 records. But it fetches a record.
EDITED :

$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


Why?

Answer

You're not specifying a column.

whereDate('added_to_cart_at', $carbon)

Comments