carbon subtract days from date code example

Example 1: carbon two day ago

$dt  =Carbon::now();

echo $dt->subDay();                      // 2012-03-03 00:00:00
echo $dt->subDays(29);

Example 2: carbon months between dates

$to = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', '2015-5-5 3:30:34');
$from = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', '2016-6-6 9:30:34');
$diff_in_months = $to->diffInMonths($from);
print_r($diff_in_months); // Output: 1

Example 3: carbon subdays

$users = Users::where('status_id', 'active')
           ->where( 'created_at', '>', Carbon::now()->subDays(30))
           ->get();

Tags:

Php Example