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
public DateTime::setTimezone(DateTimeZone $timezone): DateTime
Shembull
Le të vendosim zonën kohore 'America/New_York' për datën aktuale:
<?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:
'2023-11-15 10:30:00'
Shembull
Le të ndryshojmë zonën kohore të një objekti ekzistues DateTime:
<?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:
'2023-01-01 18:00:00'
Shihni gjithashtu
-
funksionin
date,
i cili formaton datën/kohën -
funksionin
date_default_timezone_set,
i cili vendos zonën kohore të paracaktuar -
klasën
DateTimeZone,
e cila përfaqëson një zonë kohore