Tag Archives: Time

strftime

It formats a local time/date according to locale settings
Example

<?php
setlocale(LC_TIME, “C”);
echo strftime(“%A”);
?>

Below given are the the formats

%a,%A,%d,%e,%j,%u,%w,%U,%V,%W,%b,%B,%h,%m,%C,%g,%G,%y,%Y,%H,%I,%l,%M,%p,%P,%r,%R,%S,%T,%X,%z,%Z

Output

Wednesday

gmstrftime

it formats a GMT/UTC time/date according to locale settings
Example

<?php
setlocale(LC_TIME, ‘en_US’);
echo strftime(“%b %d %Y %H:%M:%S”, mktime(17, 0, 0, 12, 31, 97)) . “<br />”;
echo gmstrftime(“%b %d %Y %H:%M:%S”, mktime(17, 0, 0, 12, 31, 97)) ;
?>

Output

Dec 31 1997 17:00:00
Dec 31 1997 11:30:00

date_timezone_set

Its an alias of DateTime::setTimezone()and  sets the time zone for the DateTime object
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

DateTime::setTimezone

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