Warning: create_function() has been disabled for security reasons in /home/veewhy/public_html/wordpress/wp-content/plugins/awesome-google-adsense/awesome-google-adsense.php on line 358

Warning: create_function() has been disabled for security reasons in /home/veewhy/public_html/wordpress/wp-content/plugins/social-sharing-toolkit/includes/share.widget.php on line 76

Warning: create_function() has been disabled for security reasons in /home/veewhy/public_html/wordpress/wp-content/plugins/social-sharing-toolkit/includes/follow.widget.php on line 40

Warning: create_function() has been disabled for security reasons in /home/veewhy/public_html/wordpress/wp-content/plugins/syntax-highlighter-compress/syntax-highlighter-compress.php on line 418

Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /home/veewhy/public_html/wordpress/wp-includes/class-wp-hook.php on line 286

Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /home/veewhy/public_html/wordpress/wp-includes/class-wp-hook.php on line 286

Warning: Cannot modify header information - headers already sent by (output started at /home/veewhy/public_html/wordpress/wp-content/plugins/awesome-google-adsense/awesome-google-adsense.php:358) in /home/veewhy/public_html/wordpress/wp-includes/feed-rss2.php on line 8
PHP – Journey Through Code http://blog.edillingham.com Observe, analyze, and experiment. Tue, 14 Jan 2020 21:38:27 +0000 en-US hourly 1 https://wordpress.org/?v=5.1.4 Quick conversion from UTC to local time in PHP http://blog.edillingham.com/2016/02/quick-conversion-from-utc-to-local-time-in-php/ http://blog.edillingham.com/2016/02/quick-conversion-from-utc-to-local-time-in-php/#respond Mon, 15 Feb 2016 17:50:03 +0000 http://blog.edillingham.com/?p=155 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.

]]>
http://blog.edillingham.com/2016/02/quick-conversion-from-utc-to-local-time-in-php/feed/ 0
Convert a constant value to its string name in PHP http://blog.edillingham.com/2016/01/convert-a-constant-value-to-its-string-name-in-php/ http://blog.edillingham.com/2016/01/convert-a-constant-value-to-its-string-name-in-php/#respond Thu, 21 Jan 2016 19:02:44 +0000 http://blog.edillingham.com/?p=151 getConstants()); echo $constants[1]; // output is "MY_CONSTANT" If […]]]> 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 you’re in an instance method and want to retrieve the constants of the current class, use get_class($this) here for the ReflectionClass constructor argument.

]]>
http://blog.edillingham.com/2016/01/convert-a-constant-value-to-its-string-name-in-php/feed/ 0