A PHP script for converting between different datetimes

This script converts a Unix timestamp (Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)) to the International Standard ISO 8601 date and time notation (YYYY-MM-DD hh:mm:ss) and vice versa. I find it useful for verifying datetimes.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Unix <-> DateTime</title>
<style type="text/css">
#wrapper {
	width: 600px;
	margin: 20px auto 0;
	font: 1.2em Verdana, Arial, sans-serif;
}
input {
	font-size: 1em;
}
#submit {
	padding: 4px 8px;
}
</style>
</head>

<body>

<div id="wrapper">

<?php 
	$convert = htmlspecialchars($_GET['convert'], ENT_QUOTES);
	$timestamp = $_POST['timestamp'];
	$datetime = $_POST['datetime'];

	if ($timestamp) {
		echo "International Standard ISO 8601 date and time notation:<br />";
		echo date('Y-m-d H:i:s', $timestamp) . " (YYYY-MM-DD hh:mm:ss)<br />";
		echo "<br />";
		echo "US Time:<br />";
		echo date('n/d/y h:i:s A', $timestamp);
	} elseif ($datetime) {
		echo "Unix Timestamp: " . $newdatetime = strtotime($datetime);
		echo "<br />";
		echo "<br />";
		echo "International Standard ISO 8601 date and time notation:<br />";
		echo date('Y-m-d H:i:s', $newdatetime) . " (YYYY-MM-DD hh:mm:ss)<br />";
		echo "<br />";
		echo "US Time:<br />";
		echo date('n/d/y h:i:s A', $newdatetime);
	} else { ?>
	
	<h1>Timestamp Conversions</h1>
	
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<table cellpadding="2">
		<tr>
			<td>Unix Timestamp</td>
			<td><input type="text" name="timestamp" id="timestamp" value="" size="30" tabindex="1" /></td>
		</tr>
		<tr>
			<td>Any Date/Time</td>
			<td><input type="text" name="datetime" id="datetime" value="" size="30" tabindex="1" /></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" id="submit" value="Convert" tabindex="5" /></td>
		</tr>
	</table>
</form>

<?php } ?>

</div>

</body>
</html>
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Technorati
  • LinkedIn
  • TwitThis
  • Google
  • E-mail this story to a friend!

Leave a reply

Comments are moderated

Wrap code snippets in <code></code> tags.