Klint
Klint

Reputation: 114

Unknown or bad timezone () in laravel

code:

public function attendance()
{
    $company = admin()->company;
    if ($company->office_start_time != NULL && $company->office_end_time != NULL) {
        $this->officeStartTime = $company->getOfficeStartTime()->timezone($company->timezone)->format('g:i A');
        $this->officeEndTime = $company->getOfficeEndTime()->timezone($company->timezone)->format('g:i A');
    }
    return \View::make('admin.company_settings.attendance', $this->data);
}

Model: company.php

<?php
namespace App\Models;
use App\Observers\CompanyObserver;
use Carbon\Carbon;
use Laravel\Cashier\Billable;
class Company extends \Eloquent
{
    use Billable;
    protected $fillable = ['company_name','contact','address','name','email','country','timezone','logo','locale','billing_address','currency','currency_symbol'];
    protected $dates = ['deleted_at', 'last_login', 'trial_ends_at', 'subscription_ends_at', 'licence_expire_on'];
    protected $appends = ['logo_image_url'];

    public function getTimezoneAttribute($value)
    {
        return explode("=", $value)[0];
    }

    public function getTimezoneIndexAttribute()
    {
        return explode("=", $this->attributes["timezone"])[1];
    }

    public function getOfficeEndTime(Carbon $date = null)
    {
        if ($date == null) {
            $date = Carbon::now();
        }
        $dateStr = $date->format("Y-m-d");
        $end = Carbon::createFromFormat("Y-m-d H:i:s", $dateStr . " " . $this->attributes["office_end_time"]);
        $start = Carbon::createFromFormat("Y-m-d H:i:s", $dateStr . " " . $this->attributes["office_start_time"]);
        if ($end < $start) {
            $end->addDay();
        }
        return $end;
    }
    public function getOfficeStartTime(Carbon $date = null)
    {
        if ($date == null) {
            $date = Carbon::now();
        }
        $dateStr = $date->format("Y-m-d");
        $start = Carbon::createFromFormat("Y-m-d H:i:s", $dateStr . " " . $this->attributes["office_start_time"]);
        return $start;
    }
}

I am new in laravel I have got InvalidArgumentException Unknown or bad timezone () in carbon.php I have got throw new InvalidArgumentException('Unknown or bad timezone ('.$object.')');. I had already set timezone in config/app.php. Once I click on tab then I got this issue I don't have any idea about this. So, How can I solve this issue? Please help me.

Thank You

Upvotes: 0

Views: 3636

Answers (1)

phpdroid
phpdroid

Reputation: 1663

This is because in the line below

$this->officeStartTime = $company->getOfficeStartTime()->timezone($company->timezone)->format('g:i A');

$company->timezone company's timezone is not set or wrong value is set

also

$this->officeStartTime = $company->getOfficeStartTime()->timezone($company->timezone!=''?$company->timezone:'somehardcodedtimezone')->format('g:i A');

Upvotes: 1

Related Questions