Category Archives: PHP

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 = […]

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 […]