Skip to content

Instantly share code, notes, and snippets.

@samhavens
Last active August 29, 2015 13:59
Show Gist options
  • Save samhavens/10685940 to your computer and use it in GitHub Desktop.
Save samhavens/10685940 to your computer and use it in GitHub Desktop.
A manual Unix timestamp converter
<!DOCTYPE html>
<html>
<head>
<title>Date</title>
</head>
<body>
<h1>
<?php
//get time from server, call it $n
$n = $_SERVER['REQUEST_TIME_FLOAT'];
//figure out what day of the year it is
//Doesn't account for leap years yet
$d = (floor($n / (3600 * 24)) % 365) + 1;
//how many years since 1970?
$y = floor($n / (3600 * 24 * 365));
$year = 1970 + $y;
//every leap year added one to our day count
//so remove it here
$d -= ($y / 4);
//is it currently a leap year?
//store as 1 or 0
$leap_year = 0;
if($year % 4 == 0) {
$leap_year = 1;
}
//figure out the month from the day number
$month;
if ($d <= 31) {
$month = 1;
}
elseif (31 < $d && $d <= 59 + $leap_year) {
$month = 2;
$d -= 31;
}
elseif (59 < $d - $leap_year && $d - $leap_year <= 90) {
$month = 3;
$d -= 59;
}
elseif (90 < $d - $leap_year && $d - $leap_year <= 120) {
$month = 4;
$d -= 90;
}
elseif (120 < $d - $leap_year && $d - $leap_year <= 151) {
$month = 5;
$d -= 120;
}
elseif (151 < $d - $leap_year && $d - $leap_year <= 181) {
$month = 6;
$d -= 151;
}
elseif (181 < $d - $leap_year && $d - $leap_year <= 212) {
$month = 7;
$d -= 181;
}
elseif (212 < $d - $leap_year && $d - $leap_year <= 243) {
$month = 8;
$d -= 212;
}
elseif (243 < $d - $leap_year && $d - $leap_year <= 274) {
$month = 9;
$d -= 243;
}
elseif (274 < $d - $leap_year && $d - $leap_year <= 305) {
$month = 10;
$d -= 274;
}
elseif (305 < $d - $leap_year && $d - $leap_year <= 335) {
$month = 11;
$d -= 305;
}
elseif ($d > 335) {
$month = 12;
$d -= 335;
}
echo "The date is " . $month . "/" . $d . "/" .$year . "<br>";
?>
</h1>
<?php
// Create a shorthand version of the
//variable names:
$file = $_SERVER['SCRIPT_FILENAME'];
$user = $_SERVER['HTTP_USER_AGENT'];
$server = $_SERVER['SERVER_SOFTWARE'];
// Print the name of this script:
echo "<p>You are running the file:<br /><b>$file</b>.</p>\n";
// Print the user's information:
echo "<p>You are viewing this page
using:<br /><b>$user</b></p>\n";
// Print the server's information:
echo "<p>This server is running:
<br /><b>$server</b>.</p>\n";
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment