Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gwomac1's avatar

DateTimeZone::__construct(): Unknown or bad timezone (Asia/Calcutta)

hello i have this code on my site but im get error /** * Get Date time Formatted *
*/ public function getDateTimeAttribute() { $now = new DateTime; $now->setTimezone(new \DateTimeZone($this->attributes['timezone'])); $ago = new DateTime($this->attributes['created_at']); $now->setTimezone(new \DateTimeZone($this->attributes['timezone'])); $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

/**
 * Get Formatted Drop Latitude
 *  
 */
public function getFormattedDropLatitudeAttribute()
{
    $latitude = $this->drop_latitude;
    if($this->trips && in_array($this->trips->status,['Rating','Payment','Completed'])) {
        $latitude = $this->trips->drop_latitude;
    }
    return $latitude;
}

/**
 * Get Formatted Drop Longitude
 *  
 */
public function getFormattedDropLongitudeAttribute()
{
    $longitude = $this->drop_longitude;
    if($this->trips && in_array($this->trips->status,['Rating','Payment','Completed'])) {
        $longitude = $this->trips->drop_longitude;
    }
    return $longitude;
0 likes
2 replies
tykus's avatar

Try Asia/Kolkata instead; it should be the correct identifier for that timezone.

Otherwise, check the contents of DateTimeZone::listIdentifiers() to see if you have an up to date list!

1 like

Please or to participate in this conversation.