Example
<?php
$date = date_create(‘2000-01-01’, timezone_open(‘Asia/Calcutta’));
echo date_format($date, ‘Y-m-d H:i:sP’) . “<br />”;
date_timezone_set($date, timezone_open(‘Australia/Perth’));
echo date_format($date, ‘Y-m-d H:i:sP’) ;
?>
Output
2000-01-01 00:00:00+05:30
2000-01-01 02:30:00+08:00
It sets the time zone for the DateTime object
Example
<?php
$date1 = new DateTime(‘2000-01-01’, new DateTimeZone(‘Asia/Calcutta’));
echo $date1->format(‘Y-m-d H:i:sP’) . “<br />”;
$date1->setTimezone(new DateTimeZone(‘Australia/Perth’));
echo $date1->format(‘Y-m-d H:i:sP’) ;
?>
Output
2000-01-01 00:00:00+05:30
2000-01-01 02:30:00+08:00
Example
<?php
$date1 = date_create(null, timezone_open(‘Asia/Calcutta’));
$timezone = date_timezone_get($date1);
echo timezone_name_get($timezone);
?>
Output
Asia/Calcutta
It return time zone relative to given DateTime
Example
<?php
$date1 = new DateTime(null, new DateTimeZone(‘Asia/Calcutta’));
$timezone = $date1->getTimezone();
echo $timezone->getName();
?>
Output
Asia/Calcutta
It sets the date and time based on an Unix timestamp .
Example
<?php
$date = new DateTime();
echo $date->format(‘U = Y-m-d H:i:s’) . “<br />”;
$date->setTimestamp(1181503727);
echo $date->format(‘U = Y-m-d H:i:s’) ;
?>
Output
1335329861 = 2012-04-25 10:27:41
1181503727 = 2007-06-11 00:58:47
Its an alias of DateTime::setTimestamp() and sets the date and time based on an Unix timestamp .
Example
<?php
$date1 = date_create();
echo date_format($date1, ‘U = Y-m-d H:i:s’).”<br />”;
date_timestamp_set($date1, 1171502325);
echo date_format($date1, ‘U = Y-m-d H:i:s’);
?>
Output
1335329740 = 2012-04-25 10:25:40
1171502325 = 2007-02-15 06:48:45
It gets the Unix timestamp
Example
<?php
$date1 = new DateTime();
echo $date1->getTimestamp();
?>
Output
1335328144
Example
<?php
$date1 = date_create();
echo date_timestamp_get($date1);
?>
Output
1335328034
It sets the time
Example
<?php
$date = new DateTime(‘2001-05-17’);
$date->setTime(12, 52);
echo $date->format(‘Y-m-d H:i:s’);
?>
Output
2001-05-17 12:52:00
Example
<?php
$date = date_create(‘2001-05-17’);
date_time_set($date, 17, 54);
echo date_format($date, ‘Y-m-d H:i:s’) . “n”;
?>
Output
2001-05-17 17:54:00
Zend | Magento Certified PHP | eCommerce Architect