PHP

Format in Engineering Notation

If you have a value and SI unit – for example 2000W – and you want to display this in engineering notation – 2kW – then this may help. /** * Format a float in engineering notation * @value float floating point number * @units string like W for Watt * @return array */ function Eng ($value, $units) { // Exceptions if (is_null($value)) { return array( ‘value’ => NULL, ‘units’ => $units, ‘decimals’ => 0,…

Continue reading

Linux MongoDB PHP

Install MongoDB with Nginx + PHP on Ubuntu

Assuming you already have Nginx and PHP installed and want to add MongoDB support. Install pre-requisites apt-get update apt-get install php5-cli php5-common php5-suhosin php5-cgi php5-curl php5-fpm php5-json php5-mcrypt php5-mysql php5-sqlite php5-dev php-pear php-apc Enable Mongo pecl install mongo echo “extension=mongo.so” >> /etc/php5/fpm/php.ini Install MongoDB apt-get install mongodb mongodb-server Restart Services service nginx restart service php5-fpm restart done

Continue reading

PHP

PHP Date Function Variables

Common Formats I use Format Example YmdH 2012061216 j-M-Y 1-Jun-2012 l jS F, Y Saturday 1st June, 2012 Y-m-d 2012-06-01 Formatting Table for Reference format character Description Example returned values ### Day     d Day of the month, 2 digits with leading zeros 01 to 31 D A textual representation of a day, three letters Mon through Sun j Day of the month without leading zeros 1 to 31 l (lowercase ‘L’) A full…

Continue reading

PHP

Posting data to a URL with PHP

If you want to send data to a web site, you can either POST or GET. GET is easy – you just read the web page contents and pass the data as a variable in the URL. $data = array(); $data[“key”] = $value; … $url = “http://domain.com?data=” . json_encode($data); file_get_contents($url) However, URLs over 2,000 characters may cause errors in some browsers. Thus limiting the amount of data you can send. POST does not have this…

Continue reading