Quick conversion from UTC to local time in PHP

I ran across something in a work project where I was required to convert all times stored in UTC to the client’s local time zone. All of the PHP solutions involved multiple lines of code involved the DateTime and DateTimeZone classes, so in order to simplify that, I came up with the following:

$formatUTCDateTime = function($dts, $format = null, $tz = null) {
   if($format === null)
	  $format = 'Y-m-d H:i:s T';
   if($tz === null)
	  $tz = date_default_timezone_get();
   $dtz = new DateTimeZone('UTC');
   $dt = new DateTime($dts, $dtz);
   $dt->setTimezone(new DateTimeZone($tz));
   return $dt->format($format);

This method supports a default datetime format and will default the timezone to the currently configured timezone. This will be set dynamically according to the local time of the user (as set by date_default_timezone_set()) and will make the conversion process transparent.

Convert a constant value to its string name in PHP

I ran into a case where I had to get the string representation of a constant based only on its value, so a little digging around and some experimentation produced this: const MY_CONSTANT = 1; // get constant name by value $class = new ReflectionClass(‘MyClassName’); $constants = array_flip($class->getConstants()); echo $constants[1]; // output is “MY_CONSTANT” If […]

A Diamond in the Rough? In a sense…

I ran across this gem tonight while trying to figure out why Visual Studio 2010 has crashed on me ten times in under an hour.  The discussion is primarily focused on the compatibility of the AnhkSVN SCC pluin, albeit with a few side tangents. It was the last comment, however, which made the hours of […]

Senet 1.0

I’ve been experimenting with the CraftyJS JavaScript game framework lately, and I’m happy to announce my first complete game: Senet. Senet is an ancient Egyptian game whose rules are the subject of some debate. A homebrew version for the Wii exists, but it’s somewhat incomplete and riddled with bugs. This version was born out of […]

4th of July Lassoing Demonstration FAIL

I captured a rather embarrassing performance earlier today during our town’s Independence Day parade. You can find the original YouTube posting here.

public string helloWorld();

Greetings. I’ve finally committed to something I’ve wanted to do for a long time; write about what I love to do. And what I love to do is develop software. To me, “coding”, “programming”, and “developing” are three entirely different approaches to what are fundamentally all the same task. The difference lies in the attitude […]