Created
April 4, 2014 07:46
-
-
Save aanton/9970002 to your computer and use it in GitHub Desktop.
Test timezones in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$serverTimezone = 'Europe/Madrid'; | |
$timezones = ['America/Los_Angeles', 'America/Mexico_City', 'Atlantic/Canary', 'Europe/Madrid']; | |
$tmst = time(); | |
$datetimeNow = new DateTime('now', new DateTimeZone($serverTimezone)); | |
$datetimes = array(); | |
foreach ($timezones as $timezone) | |
{ | |
$datetime = new DateTime(); | |
$datetime->setTimestamp($tmst); | |
$datetime->setTimezone(new DateTimeZone($timezone)); | |
$datetimes[] = $datetime; | |
} | |
// Client timestamp demo | |
// Input parameters: tmst1 | |
$tmst1 = $_GET['tmst1'] ?: $tmst; | |
$datetimes1 = array(); | |
foreach ($timezones as $timezone) | |
{ | |
$datetime = new DateTime(); | |
$datetime->setTimestamp($tmst1); | |
$datetime->setTimezone(new DateTimeZone($timezone)); | |
$datetimes1[] = $datetime; | |
} | |
// Client date demo | |
// Input parameters: date2, timezone2 | |
$date2 = $_GET['date2'] ?: $datetimeNow->format('Y-m-d H:i:s'); | |
$timezone2 = $_GET['timezone2'] ?: $serverTimezone; | |
$datetime2 = new DateTime($date2, new DateTimeZone($timezone2)); | |
$tmst2 = $datetime2->getTimestamp(); | |
$datetimes2 = array(); | |
foreach ($timezones as $timezone) | |
{ | |
$datetime = new DateTime(); | |
$datetime->setTimestamp($tmst2); | |
$datetime->setTimezone(new DateTimeZone($timezone)); | |
$datetimes2[] = $datetime; | |
} | |
?> | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Timezones</title> | |
</head> | |
<body> | |
<h1>Timezones</h1> | |
<a href="timezones.php">Reset</a> | |
<h2>Current timestamp</h2> | |
<p>Timestamp: <?php echo $tmst; ?></p> | |
<ul> | |
<?php foreach ($datetimes as $datetime): ?> | |
<li><?php echo $datetime->format(DateTime::ISO8601); ?> (<?php echo $datetime->format('e'); ?>)</li> | |
<?php endforeach; ?> | |
</ul> | |
<hr> | |
<h2>Client timestamp demo</h2> | |
<form action="timezones.php" method="GET"> | |
<label>Timestamp: <input type="text" name="tmst1" value="<?php echo $tmst1; ?>" /></label> | |
<input type="hidden" name="date2" value="<?php echo $date2; ?>" /><!-- keep date demo --> | |
<input type="hidden" name="timezone2" value="<?php echo $timezone2; ?>" /><!-- keep date demo --> | |
<input type="submit" value="Submit" /> | |
</form> | |
<ul> | |
<?php foreach ($datetimes1 as $datetime): ?> | |
<li><?php echo $datetime->format(DateTime::ISO8601); ?> (<?php echo $datetime->format('e'); ?>)</li> | |
<?php endforeach; ?> | |
</ul> | |
<h2>Client date demo</h2> | |
<form action="timezones.php" method="GET"> | |
<label> | |
Date: | |
<input type="text" name="date2" value="<?php echo $date2; ?>" /> | |
</label> | |
<label> | |
Timezone: | |
<select name="timezone2"> | |
<?php foreach ($timezones as $timezone): ?> | |
<option value="<?php echo $timezone; ?>" <?php echo $timezone === $timezone2 ? 'selected' : ''; ?>><?php echo $timezone; ?></option> | |
<?php endforeach; ?> | |
</select> | |
</label> | |
<input type="hidden" name="tmst1" value="<?php echo $tmst1; ?>" /><!-- keep timestamp demo --> | |
<input type="submit" value="Submit" /> | |
</form> | |
<p>Timestamp: <?php echo $tmst2; ?></p> | |
<ul> | |
<?php foreach ($datetimes2 as $datetime): ?> | |
<li> | |
<?php if ($datetime->format('e') === $timezone2): ?><strong><? endif; ?> | |
<?php echo $datetime->format(DateTime::ISO8601); ?> (<?php echo $datetime->format('e'); ?>) | |
<?php if ($datetime->format('e') === $timezone2): ?></strong><? endif; ?> | |
</li> | |
<?php endforeach; ?> | |
</ul> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment