Category Archives: Uncategorized

How to use Highlight.js to highlight code

Using Highlight.js to highlight code is simple: First include this inside the HEAD tag of the HTML page: <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.14.2/highlight.min.js"></script> <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.14.2/styles/darcula.min.css" /> <script>hljs.initHighlightingOnLoad();</script><script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.14.2/highlight.min.js"></script> <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.14.2/styles/darcula.min.css" /> <script>hljs.initHighlightingOnLoad();</script> Then you can use it like this: <code class="json"> {name: "John", age: 31, city: "New York"} </code><code class="json"> {name: "John", age: 31, city: "New […]

Bootstrap 3 Navbar with Logo Image

The correct way to use a logo image on the Navbar on Bootstrap 3 is: <a href="#" class="navbar-left"><img src="/path/to/image.png"></a><a href="#" class="navbar-left"><img src="/path/to/image.png"></a> Do not use class=”navbar-brand” in A HREF because it adds text-like styles. Reference: https://stackoverflow.com/a/26333342 If the logo image is too on top, just add this code in the .CSS file: .navbar-default .navbar-left img […]

PHP GetHostByName() Slow? Make it Faster

Sometimes gethostbyname() can take up to 5 seconds and more to complete. Here are two smart tricks to make it faster: // Set timeout and retries to 1 to have a max execution time of 1 second for the DNS lookup putenv(‘RES_OPTIONS=retrans:1 retry:1 timeout:1 attempts:1’);   // Make sure the domain ends in a dot […]

Use Timestamp with new DateTime() in PHP

You can use a unix timestamp within DateTime() like this: $today = new DateTime("@".time()); $tomorrow = new DateTime("@".strtotime("+13 months"));$today = new DateTime("@".time()); $tomorrow = new DateTime("@".strtotime("+13 months")); See that we prepend “@” char before the timestamp.

Calculate Elapsed Time Between Two Dates in PHP

This function is used to calculate elapsed time between two dates: function formatDateDiff($start, $end=null, $ago=" ago", $left=" left") { if(!($start instanceof DateTime)) { $start = new DateTime($start); }   if($end === null) { $end = new DateTime(); }   if(!($end instanceof DateTime)) { $end = new DateTime($start); }   $interval = $end->diff($start);   $elapsedtype = […]

PHP Display Date Same as Server HTTP Headers Date

Display date same as server’s HTTP Headers date: $date = gmdate(‘D, d M Y H:i:s T’, time());$date = gmdate(‘D, d M Y H:i:s T’, time()); Example output: Tue, 29 Jan 2019 13:23:38 GMTTue, 29 Jan 2019 13:23:38 GMT

PHP Library for Google Authenticator (Two Factor Authentication 2FA)

Here is a very simple and working PHP library for Google Authenticator: https://github.com/PHPGangsta/GoogleAuthenticator With that library you can easily integrate two factor (2FA) authentication like this: require_once ‘PHPGangsta/GoogleAuthenticator.php’;   $ga = new PHPGangsta_GoogleAuthenticator();   // Save this $secret in a database and load it from the database the next time // The $secret variable should […]

How to Validate JSON in PHP

Simple PHP function to validate JSON in PHP: function is_valid_json($string) { return !empty($string) && is_string($string) && is_array(json_decode($string, true)) && json_last_error() === 0; }function is_valid_json($string) { return !empty($string) && is_string($string) && is_array(json_decode($string, true)) && json_last_error() === 0; } Alternatively you can do like this (doing only one json_decode() call): $output = json_decode($string, true);   if (!is_array($output) […]

PHP CURL Download Remote URL to File

Simple PHP snippet to download a remote URL to a file: function curl_download_to_file( $url, $file ) { $fp = fopen($file, ‘w+’); $curl = curl_init(); curl_setopt ($curl, CURLOPT_URL, $url); curl_setopt ($curl, CURLOPT_TIMEOUT, 10); curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1"); curl_setopt ($curl, CURLOPT_HEADER, 0); curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); […]

PHP Check if a file was modified more than 1 hour ago

Simple PHP snippet to check if a file was last modified more than 1 hour ago: if(time() – filemtime("/path/to/file") >= 1 * 3600) { echo "File was modified more than 1 hour ago"; }if(time() – filemtime("/path/to/file") >= 1 * 3600) { echo "File was modified more than 1 hour ago"; } Check if a file […]