This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Andydrummmer's profile picture

Computercraft Time Server - Real World Time

Started by Andydrummmer, 18 August 2014 - 01:50 AM
Andydrummmer #1
Posted 18 August 2014 - 03:50 AM
This is a simple PHP script to fetch time in a wide variety of formats.
The PHP file is hosted at http://www.cctime.cu9.co/cctime.php.
The server it is hosted on should be reliable.

The three main formats are as follows:
  • Epoch - Unix Timestamp Example: 1408328148
  • Basic - a serialized array of basic time value. Example: {month=8,day=17,year=2014,hour12=8,hour24=20,minute=14,second=05,meridiem=PM,}
  • Custom - this allows you to create your own format. Explained below.
The Custom format passes the "format" variable from the URL directly into the date() function in PHP. Use "%" for spaces when creating a format this way.
Example: http://www.cctime.cu...mat=g:i%A%m/d/Y this outputs something like this –> 2:26 AM 08/18/2014
You can create your own by using the table shown here. Each letter represents a time value.

When you enter the URL, you must enter a format and a offset(Timezone difference from UTC). The offset can be negative or positive.

I have used this on an Apache server running on the same server as the minecraft server. This way you could fetch time locally.

I may replace the offset with timezones and make it adjust for things such as Daylight Savings Time in the future.

Example URLs:
SpoilerEpoch:
http://www.cctime.cu...=0&format=epoch
Basic:
http://www.cctime.cu...0&format=serial
Custom:
http://www.cctime.cu...mat=g:i%A%m/d/Y

Use and modify this PHP file as you wish.

PHP File:
Spoiler<?php
date_default_timezone_set ("UTC");
//User Defined Variables (UDV)
$offset = $_GET['offset'];
$format = $_GET['format'];
//Checks if UDV's are given
if (isset($offset) == 0){
echo("The offset is not defined.");
echo("\n");
}
if (isset($format) == 0){
echo("The format is not defined.");
}

/*
Time Formats:
Epoch
serial
custom
*/
//Epoch
if ($format == "epoch"){
echo time();
}
//serial
if ($format == "serial"){
$offsetstamp = time() + ($offset * 60 * 60);
$day = date("j", $offsetstamp);
$month = date("n", $offsetstamp);
$year = date("Y", $offsetstamp);
$second = date("s", $offsetstamp);
$meridiem = date("A", $offsetstamp);
$hour24 = date("G", $offsetstamp);
$hour12 = date("g", $offsetstamp);
$minute = date("i", $offsetstamp);
$serialized = "{"."month=".$month.","."day=".$day.","."year=".$year.","."hour12=".$hour12.","."hour24=".$hour24.","."minute=".$minute.","."second=".$second.","."meridiem=".$meridiem.",}";
echo $serialized;
}
//custom
if ($format != "serial"){
if($format != "epoch"){
$offsetstamp = time() + ($offset * 60 * 60);
$output = str_replace("%"," ",$format);
$date = date($output, $offsetstamp);
echo $date;
}
}
?>
newcat #2
Posted 19 August 2014 - 10:13 PM
Pretty simple and pretty powerful! Awesome
Zambonie #3
Posted 19 August 2014 - 10:17 PM
What server is the hosted on?
Either the website is down, Or its hosted on 000webhost. (Many ISPs Blacklist 000webhost websites, Including Mine.)
Edited on 19 August 2014 - 08:18 PM
Andydrummmer #4
Posted 22 August 2014 - 10:57 PM
It is hosted on a free server from Freeost.com. If I find another place to host it, I will post the link here.