// there are 86.4 seconds in a beat
var millisecondsInABeat = 86400;
// calculate beats to two decimal places
if (showDecimals) {
return Math.abs(timeInMilliseconds / millisecondsInABeat).toFixed(2);
} else {
return Math.floor(Math.abs(timeInMilliseconds / millisecondsInABeat));
}
}
</code>
If you want it to display on your webpage and auto-update here is an example - first, we make a span with an id, this is where the clock will appear on your page - then we get that span in a script, make a function that fills it with the swatch time, and finally, we set it to reload every microbeat.
<span id="mySwatchClock"></span>
<script>
var mySwatchClock = document.getElementById('mySwatchClock');
function updateSwatchClock() {
mySwatchClock.innerHTML = '@' + GetSwatchTime();
}
setInterval(updateSwatchClock(), 864);
</script>
==== PHP: ====
PHP has native support for Swatch time, you can get the latest beat using this snippet:
$swatchTime = date('B'); //000-999
However if you would like a full function that also shows microbeats, we have that too!
// Returns the current Swatch beat
function GetSwatchTime($showDecimals = true)
{
// Get time in UTC+1 (Do not Change!)
$now = new DateTime("now", new DateTimeZone("UTC"));
$now->add(new DateInterval("PT1H"));
// Calculate the seconds since midnight e.g. time of day in seconds
$midnight = clone $now;
$midnight->setTime(0, 0);
$seconds = $now->getTimestamp() - $midnight->getTimestamp();
// Swatch beats in seconds - DO NOT CHANGE
$swatchBeatInSeconds = 86.4;
// Calculate beats to two decimal places
if ($showDecimals) {
return number_format(round(abs($seconds / $swatchBeatInSeconds), 2), 2);
} else {
return floor(abs($seconds / $swatchBeatInSeconds));
}
}
