Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep getting Unknown or bad timezone from Carbon Laravel

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?

like image 849
Antonio Avatar asked Oct 19 '25 10:10

Antonio


1 Answers

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);
}
like image 126
nextt1 Avatar answered Oct 22 '25 00:10

nextt1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!