I'm trying to add 2 months to a date with Carbon, but I keep getting this error:
InvalidArgumentException in Carbon.php line 252:
Unknown or bad timezone (2017-02-25 14:25:24)
Here is my code:
<?php
public function giveMonths(Request $request)
{
$query = DB::table('users')->where('level', '=', 2)->get();
foreach ($query as $row) {
$id = $row->id;
$expires = $row->expires;
$newDate = Carbon::now($expires)->addMonths(2);
dd($newDate);
}
In "app.php" the timezone is set this way:
'timezone' => 'America/Sao_Paulo'
The timezone stored in the DB is in this format:
2017-02-25 03:51:12
How can I solve this problem?
You are using the wrong method. you must pass a timezone as an argument while using now
. In your case you should use parse
method.
$query = DB::table('users')->where('level', '=', 2)->get();
foreach ($query as $row)
{
$id = $row->id;
$expires = $row->expires;
$newDate = Carbon::parse($expires)->addMonths(2);
dd($newDate);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With