Time Server – Code Snippets

      No Comments on Time Server – Code Snippets
Intro | Documentation  | Code Snippets Form

HTML

The easiest way to add the Thelemic date to a website is to use the time server’s image output:
e.g., <img src="http://www.lashtal.com/thelemic_date/date.php" alt="" title="Thelemic Date" />

JavaScript

This should include the client-side Timezone setting in JavaScript if the weekday is required.  Note that there is a <noscript> tag for users without JavaScript, and an alt="" attribute to prevent a mess if the image fails to load for whatever reason:

<script type="text/javascript" language="JavaScript">
<!--
var d = new Date()
var timezone = -d.getTimezoneOffset()
document.write('
<a href="http://www.lashtal.com/thelemic_date/" title="LAShTAL.com Thelemic Time Server"><img src="http://www.lashtal.com/thelemic_date/date.php?weekday=1&timezone='+timezone+'" alt="" title="Thelemic Date" /></a>')
/-->
</script>
<noscript>
<img src="http://www.lashtal.com/thelemic_date/date.php" alt="" title="Thelemic Date" />
</noscript>

PHP

Following on from the documentation on the XML output, the main function of the XMLproxy.php file might look something like this in PHP 5:

function getThelemicDate($timezone) {
error_reporting(0);
// Get the XML data from lashtal.com:
if(!$fp = @fsockopen("www.lashtal.com", 80, $errno, $errstr, 0.15)) return false;
if (!
$fh = @fopen("http://www.lashtal.com/thelemic_date/date.php?output=xml&;timezone=$timezone", 'r')) return false;
stream_set_blocking($fh,true);
stream_set_timeout($fh, 1);
$data = @stream_get_contents($fh);
$status = socket_get_status($fh);
if (
$status['timed_out']) return false;
fclose($fh);
fclose($fp);
// Parse the XML:
if (!$xml = @simplexml_load_string($data)) return false;
// Return an <img> tag using our custom image files:
$output = "<a href=\"http://www.lashtal.com/thelemic_date/\" title=\"LAShTAL.com LAShTAL.com Thelemic Time Server\"><img src=\"sol.gif\" alt=\"Sol\" /> in <img src=\"{$xml->SunSign->Text}.gif\" alt=\"{$xml->SunSign->Text}\" /> : <img src=\"luna.gif\" alt=\"Luna\" /> in <img src=\"{$xml->MoonSign->Text}.gif\" alt=\"{$xml->MoonSign->Text}\" /> : Anno {$xml->Year->Decimal} a.n.</a>";
return
$output;
}

The same function in PHP 4 (using the MiniXML class library) would look something like this:

function getThelemicDate($timezone) {
error_reporting(0);
require_once(
"miniXML/minixml.inc.php");
// Get the XML data from lashtal.com:
if(!$fp = @fsockopen("www.lashtal.com", 80, $errno, $errstr, 0.15)) return false;
$out = "GET http://www.lashtal.com/thelemic_date/date.php?output=xml&;timezone=$timezone HTTP/1.0\r\n";
$out .= "Host: www.lashtal.com\r\n\r\n";
fputs($fp, $out);
stream_set_blocking($fp,true);
stream_set_timeout($fp, 1);
$status = socket_get_status($fp);
while (!
feof($fp) and !$status['timed_out']) {
$data .= @fgets($fp, 1024);
$status = socket_get_status($fp);
}
if (
$status['timed_out']) return false;
fclose($fp);
list(
$headers,$data) = split("\r\n\r\n", $data, 2);
$data = trim($data);
// Parse the XML:
$xmlDoc = new MiniXMLDoc();
$xmlDoc->fromString($data);
$xml = $xmlDoc->getRoot();
$sun_sign = $xml->getElementByPath('ThelemicDate/SunSign/Text');
$sun_sign = $sun_sign->GetValue();
$moon_sign = $xml->getElementByPath('ThelemicDate/MoonSign/Text');
$moon_sign = $moon_sign->GetValue();
$decimal_year = $xml->getElementByPath('ThelemicDate/Year/Decimal');
$decimal_year = $decimal_year->GetValue();
// Return an <img> tag using our custom image files:
$output = "<a href=\"http://www.lashtal.com/thelemic_date/\" title=\"LAShTAL.com LAShTAL.com Thelemic Time Server\"><img src=\"sol.gif\" alt=\"Sol\" /> in <img src=\"$sun_sign.gif\" alt=\"$sun_sign\" /> : <img src=\"luna.gif\" alt=\"Luna\" /> in <img src=\"$moon_sign.gif\" alt=\"$moon_sign\" /> : Anno $decimal_year a.n.</a>";
return
$output;
}

Note that both functions contain two separate timeout checks, one on the www.lashtal.com host and one on the filestream itself.  If using PHP 5, the first one of these can be removed from the function without affecting execution, but it’s there because the microtime portion of the stream_set_timeout() function doesn’t seem to work properly in PHP 4 or PHP 5 (therefore the minimum value is 1 second), so a shorter timeout can only be implemented through the fsockopen() floating-point timeout value.

The XML file will always be less than 1024 bytes (including checksums), so the transmission time should be short; but nevertheless the fsockopen() timeout may need to be adjusted on slower or more distant servers, although – as noted – the script will not interfere with the loading of the webpage if called from an <object> or <img> tag, so really there is no need for a timeout check at all (unless the function is being called as part of a larger script), since the script will simply stop after max_execution_time is reached.  However, depending on the server configuration, it may be necessary to add the allow_url_fopen=1 directive to the php.ini file in order to allow loading of remote files in the first place.

If no timeout is needed, the process of getting the XML file into a string suitable for parsing is simpler and quicker, so instead of all the malarkey in the above scripts one can simply use the following:

In PHP 5:
if (!$xml = simplexml_load_file("http://www.lashtal.com/thelemic_date/date.php?output=xml&;timezone=$timezone")) return false;

In PHP 4:
if (!$data = file_get_contents("http://www.lashtal.com/thelemic_date/date.php?output=xml&;timezone=$timezone")) return false;

Finally, the getThelemicDate() function can now be called from within the script, and some output produced depending upon the result.  If for some reason the XML data cannot be loaded, it may be desirable to produce a common era date instead; so, putting it all together, we have:

<?php
function getThelemicDate($timezone) {
.
.
.
}
// Get the Timezone variable from the <object> tag:
$timezone = (is_numeric($_GET['timezone'])) ? intval($_GET['timezone']) : 0;
// Call the function and decide upon the output:
if (!$date = getThelemicDate($timezone)) $date = gmdate('D j M Y', time()+($timezone*60));
// The MIME type of the HTTP header should match that expected by the <object> tag:
header('Content-type: text/html; charset=iso-8859-1');
echo
$date;
?>

Reverse Lookup PHP/JavaScript

If one doesn’t wish to use the image output format or an <object> tag to display, e.g., the date of a Spring Equinox in HTML on a website, the following code may be used:

<?php
error_reporting
(0);
$months = array('Jan'=>1'Feb'=>2'Mar'=>3'Apr'=>4'May'=>5,
            
'Jun'=>6'Jul'=>7'Aug'=>8'Sep'=>9'Oct'=>10'Nov'=>11'Dec'=>12);
if(!
$fp = @fsockopen("www.lashtal.com"80$errno$errstr0.15)) exit;
$out "GET http://www.lashtal.com/thelemic_date/date.php?tdate=102-aries HTTP/1.0\r\n";
$out .= "Host: www.lashtal.com\r\n\r\n";
fputs($fp$out);
stream_set_blocking($fp,true);
stream_set_timeout($fp1);
$status socket_get_status($fp);
while (!
feof($fp) and !$status['timed_out']) {
    
$data .= @fgets($fp1024);
    
$status socket_get_status($fp);
}
if (
$status['timed_out']) exit;
fclose($fp);
list(
$headers,$data) = split("\r\n\r\n"$data2);
$data trim($data);
$gmtime gmmktime(substr($data,17,2), substr($data,20,2),
                    
substr($data,23,2), $months[substr($data,8,3)],
                        
$day substr($data,5,2), substr($data,12,4));
echo 
"<script type=\"text/javascript\" language=\"javascript\">
<!--
var d = new Date()
var tz = d.timezoneOffset();
d.setTime($gmtime*1000)
document.write('Local time of Spring Equinox: '+d.toLocaleString());
//-->
</script>
<noscript>Time of Spring Equinox: $data</noscript>"
;
?>

Subscribe
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments