Friday, March 25, 2011

Simple PHP Time Zone Conversions Example

Simple PHP Time Conversions Examples

Two procedural programming examples, then an object-oriented style last.

Example 1

(First the output, then the working code.)

Output

We are starting with a USA style date and time
##/##/## is read by the strtotime function as a USA date (mm/dd/yy)
##-##-## would be read as a "computer" date (yy-mm-dd)
##.##.## would be read as a European date (dd-mm-yy)
See the Date Formats page in the PHP manual.
3/25/2011 6:30 p.m.
First we tell PHP what time zone this date came from.
America/Chicago
Then we convert the input date to a (GMT) Unix timestamp
1301095800
Now we move in our example to a different time and place, pretending we stored the Unix timestamp earlier. We now need to get it out.
First let's tell PHP the time zone we want to see it in.
America/Los_Angeles
Next let's set a format for the strftime functions to use.
%Y-%m-%d %r %z %Z
Now we can have a look at the formatted UTC time.
2011-03-25 11:30:00 PM +0000 GMT
Next, let's have a look at the formatted time for the output time zone.
2011-03-25 04:30:00 PM -0700 PDT

PHP Code

Here is the PHP code (with br tags obfuscated):

echo "Simple PHP Time Conversions Example-br /-\n";
echo "(First the output, then the working code.)-br /-\n";
echo "-br /-\n";
echo "We are starting with a USA style date and time-br /-\n";
echo "##/##/## is read by the strtotime function as a USA date (mm/dd/yy)-br /-\n";
echo "##-##-## would be read as a \"computer\" date (yy-mm-dd)-br /-\n";
echo "##.##.## would be read as a European date (dd-mm-yy)-br /-\n";
echo "See the Date Formats page in the PHP manual.-br /-\n";
echo $input_date_time="3/25/2011 6:30 p.m.";
echo '-br /-';
echo "First we tell PHP what time zone this date came from.-br /-\n";
echo $input_time_zone='America/Chicago';
echo '-br /-';
date_default_timezone_set($input_time_zone);
echo "Then we convert the input date to a (GMT) Unix timestamp-br /-\n";
echo $utc_timestamp=strtotime($input_date_time);
echo "-br /-\n";
echo "Now we move in our example to a different time and place, pretending we stored the Unix timestamp earlier. We now need to get it out.-br /-\n";
echo "First let's tell PHP the time zone we want to see it in.-br /-\n";
echo $output_time_zone='America/Los_Angeles';
echo '-br /-';
date_default_timezone_set($output_time_zone);
echo "Next let's set a format for the strftime functions to use.-br /-\n";
echo $date_output_strf_format="%Y-%m-%d %r %z %Z";
echo '-br /-';
echo "Now we can have a look at the formatted UTC time.-br /-\n";
echo gmstrftime($date_output_strf_format,$utc_timestamp);
echo '-br /-';
echo "Next, let's have a look at the formatted time for the output time zone.-br /-\n";
echo strftime($date_output_strf_format,$utc_timestamp);
echo '-br /-';

Example 2

(First the output, then the working code.)

Output

PROCEDURAL METHOD
Timestamp:
1301207028
Phoenix:
2011-03-26 23:23:48 -0700 MST
2011-03-26 23:23:48 -25200 seconds MST America/Phoenix
Los Angeles:
2011-03-26 23:23:48 -0700 PDT
2011-03-26 23:23:48 -25200 seconds PDT America/Los_Angeles
Chicago:
2011-03-27 01:23:48 -0500 CDT
2011-03-27 01:23:48 -18000 seconds CDT America/Chicago
UTC:
2011-03-27 06:23:48 +0000 GMT


OBJECT ORIENTED METHOD
Phoenix:
2011-03-26 23:23:48 -25200 seconds MST America/Phoenix
Los Angeles:
2011-03-26 23:23:48 -25200 seconds PDT America/Los_Angeles
Chicago:
2011-03-27 01:23:48 -18000 seconds CDT America/Chicago

UTC:
2011-03-27 06:23:48 0 seconds UTC UTC

PHP Code

Here is the PHP code (with br tags obfuscated):

echo 'PROCEDURAL METHOD-br /-';
$event_time=time(); // Current timestamp stored.
echo 'Timestamp:-br /-';
echo $event_time.'-br /-';
$date_output_strf_format="%Y-%m-%d %T %z %Z";
$date_output_date_format="Y-m-d H:i:s Z \s\e\c\o\\n\d\s T e";
date_default_timezone_set('America/Phoenix');
echo 'Phoenix:-br /-';
echo strftime($date_output_strf_format,$event_time).'-br /-';
echo date($date_output_date_format,$event_time).'-br /-';

date_default_timezone_set('America/Los_Angeles');
echo 'Los Angeles:-br /-';
echo strftime($date_output_strf_format,$event_time).'-br /-';
echo date($date_output_date_format,$event_time).'-br /-';

date_default_timezone_set('America/Chicago');
echo 'Chicago:-br /-';
echo strftime($date_output_strf_format,$event_time).'-br /-';
echo date($date_output_date_format,$event_time).'-br /-';
echo 'UTC:-br /-';
echo gmstrftime($date_output_strf_format,$event_time).'-br /-';
echo '-br /-';
echo '-br /-';
echo 'OBJECT ORIENTED METHOD-br /-';

$event_time=new DateTime(); // Current timestamp
echo 'Phoenix:-br /-';
$event_time->setTimezone(new DateTimeZone('America/Phoenix'));
echo $event_time->format($date_output_date_format).'-br /-';
echo 'Los Angeles:-br /-';
$event_time->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $event_time->format($date_output_date_format).'-br /-';
echo 'Chicago:-br /-';
$event_time->setTimezone(new DateTimeZone('America/Chicago'));
echo $event_time->format($date_output_date_format).'-br /-';
echo '-br /-';
echo 'UTC:-br /-';
$event_time->setTimezone(new DateTimeZone('UTC'));
echo $event_time->format($date_output_date_format).'-br /-';

No comments: