Howto calculate Swatch internet time using PHP

date_default_timezone_set("UTC");

function internetMicrotime($get_as_float = false)
{
  //Calculate Swatch internet time

  //Get unix timestamp for the beginning of the this day
  //With 1 hour offset for the Swatch internet timezone
  //(Biel Meantime (BMT): UTC+1 without DST)
  $beginningOfDay = mktime(-1, 0, 0);

  //Calulate total SI seconds in a day
  $totalSIsecondsInDay = 60 * 60 * 24;

  //Define how many beats there are in a day
  $totalBeatsInDay = 1000;

  //SI seconds elapsed since the beginning of the day
  $secondsElapsed = microtime(true) - $beginningOfDay;

  //Calculate factor on how far this day has elapsed
  $factor = $secondsElapsed / $totalSIsecondsInDay;

  //Now calulate the decimal time using
  //the total decimal seconds and the factor
  $internetTime = $totalBeatsInDay *  $factor;

  if ($get_as_float)
    return $internetTime;
  else
    return floor($internetTime);
}

print internetMicrotime(true)."\n";  //495.81894635326
print internetMicrotime(false)."\n"; //495
print date("H:i:s")."\n";            //10:53:58

Further reading: http://www.swatch.com/zz_en/internettime/

Howto calculate local hexadecimal time using PHP

date_default_timezone_set("Europe/Amsterdam");

function hexadecimalTime()
{
  //Calculate hexadecimal local time

  //Get unix timestamp for the beginning of this day
  $beginningOfDay = mktime(0, 0, 0);

  //Calculate total SI seconds in a day
  $totalSIsecondsInDay = 60 * 60 * 24;

  //Define how many hexadecimal seconds there are in a day
  $totalHexadecimalSecondsInDay = 0xFFFF;

  //SI seconds elapsed since the beginning of the day
  $secondsElapsed = microtime(true) - $beginningOfDay;

  //Calculate factor on how far this day has elapsed
  $factor = $secondsElapsed / $totalSIsecondsInDay;

  //Now calculate the hexadecimal time using
  //the total hexadecimal seconds and the factor
  $hexadecimalTime = $totalHexadecimalSecondsInDay *  $factor;

  return dechex(floor($hexadecimalTime));
}

print hexadecimalTime()."\n"; //85fc
print date("H:i:s")."\n";     //12:33:41

Howto calculate local decimal time using PHP

date_default_timezone_set("Europe/Amsterdam");

function decimalMicrotime($get_as_float = false)
{
  //Calculate decimal local microtime

  //Get unix timestamp for the beginning of this day
  $beginningOfDay = mktime(0, 0, 0);

  //Calculate total SI seconds in a day
  $totalSIsecondsInDay = 60 * 60 * 24;

  //Define how many decimal seconds there are in a day
  $totalDecimalSecondsInDay = 100000;

  //SI seconds elapsed since the beginning of the day
  $secondsElapsed = microtime(true) - $beginningOfDay;

  //Calculate factor on how far this day has elapsed
  $factor = $secondsElapsed / $totalSIsecondsInDay;

  //Now calculate the decimal time using
  //the total decimal seconds and the factor
  $decimalTime = $totalDecimalSecondsInDay *  $factor;

  if ($get_as_float)
    return $decimalTime;
  else
    return floor($decimalTime);
}

print decimalMicrotime(true);  //89580.109306784
print decimalMicrotime(false); //89580
print date("H:i:s");           //21:29:57

PHP 5.4.0 for Centos 6.2

I have updated the PHP rpms to 5.4.0. Click here to download the rpms.

Release notes: http://www.php.net/archive/2012.php#id2012-03-01-1

The key features of PHP 5.4.0 include:

  • New language syntax including Traits, shortened array syntax and more
  • Improved performance and reduced memory consumption
  • Support for multibyte languages now available in all builds of PHP at the flip of a runtime switch
  • Built-in webserver in CLI mode to simplify development workflows and testing
  • Cleaner code base thanks to the removal of multiple deprecated language features
  • Many more improvements and fixes