Metoda setTimezone e Klasës DateTime

Metoda setTimezone ndryshon zonën kohore të objektit DateTime. Si parametër pranon një objekt të klasës DateTimeZone. Kthen objektin DateTime të modifikuar.

Sintaksa

phppublic DateTime::setTimezone(DateTimeZone $timezone): DateTime

Shembull

Le të vendosim zonën kohore 'America/New_York' për datën aktuale:

php<?php
  $date = new DateTime('now', new DateTimeZone('UTC'));
  $date->setTimezone(new DateTimeZone('America/New_York'));
  echo $date->format('Y-m-d H:i:s');
?>

Rezultati i ekzekutimit të kodit:

php'2023-11-15 10:30:00'

Shembull

Le të ndryshojmë zonën kohore të një objekti ekzistues DateTime:

php<?php
  $date = new DateTime('2023-01-01 12:00:00', new DateTimeZone('Europe/Moscow'));
  $date->setTimezone(new DateTimeZone('Asia/Tokyo'));
  echo $date->format('Y-m-d H:i:s');
?>

Rezultati i ekzekutimit të kodit:

php'2023-01-01 18:00:00'

Shihni gjithashtu