Tag: PHP

Convert Time String (HH:MM:SS) To Seconds
Sometimes it is necessary to find the amount of seconds in a time string for use in equations. Below are PHP and Python functions to do this. These functions work by splitting the time string by ‘:’, then calculating 60 to the power of the split time count minus 1 (allowing for MM:SS format and…

Adding number formatting to percentages in phpPowerPoint Charts
Below is a patch to add number formatting to percentages in phpPowerPoint Pie Charts: {{{ lang=diff Index: Shape/Chart/Series.php =================================================================== — Shape/Chart/Series.php (revision 71171) +++ Shape/Chart/Series.php (working copy) @@ 101,6 +101,15 @@ * @var boolean */ private $_showPercentage = false; + + /** + * $_percentFormat + * Added to set the number format of series…

Enumerate Processes Programmatically In Linux
Using `ps` is not necessarily the best way to enumerate running applications programmatically. This is because it will gather additional information that we do not actually need in most cases. The below examples illustrate how to enumerate running applications by reading the contents of `/proc`. **Python:** {{{ lang=python line=1 def get_procs(proc_name=None): ## Loop over /proc…

Round To Nearest X
Sometimes it is necessary to round a number to the nearest X (.1,5,10, etc). Below is some code to do that: ==PHP== {{{ lang=php line=1 # Round int to nearest $round_to function round_int($n, $round_to=10){ return round($n / $round_to) * $round_to; } # Round float to nearest $round_to, configurable rounding down function round_float($n, $round_to = .1,…

PHP Levenshtein distance
Below is my implementation of Levenshtein distance in PHP. This is useful to determine the distance between two latitude & longitude pairs. {{{ lang=php line=1 $lat1 = deg2rad($location1>lat); $lng1 = deg2rad($location1>lng); $lat2 = deg2rad($location2>lat); $lng2 = deg2rad($location2>lng); $theta = $location1>lng – $location2>lng; $dist = sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos(deg2rad($theta)); $dist =…