How to get the Hours Difference (in HH:MM format) in PHP

It’s very simple to get the difference in hours using PHP 1 2 3 4 5 6 7 8 function timeDiff($firstTime,$lastTime) { $firstTime=strtotime($firstTime); $lastTime=strtotime($lastTime); $timeDiff=$lastTime-$firstTime; return $timeDiff; } echo (timeDiff("10:00","12:00")/60)/60;

Warning: Invalid argument supplied for foreach()

A common error which occurs with foreach loops is the standard “Invalid argument supplied for foreach()” whch gets thrown up as a warning. This can easily be overcome by wrapping the foreach loop in a conditional if statement which checks to see if the argument supplied to the loop is an array or not. Below is an example of how to correct the problem: 1 2 3 4 5 if (in_array($arrItem)) { foreach($arrItem as $arrItemi) { // use $arrItemi; } }

Unable to connect to any of the specified MySQL hosts.

If you get the following error while trying to connect to a MySQL database using a third party client such as TOAD or SQL Workbench, then you need to edit your mysql server’s my.cnf file. Error Message: Unable to connect to any of the specified MySQL hosts. So open the my.cnf file by running a similar command to the following one dependent on your server’s setup: 1 vi /etc/mysql/my.cnf Then edit the bind-address from what is probably set as 127.

PHP nl2br on one line

I usually store data from a textarea directly to the database as is, then once I retrieve it back into HTML I use that lovely PHP function nl2br() to convert it to HTML tags. This works well for most cases until you are passing this data back into javascript where everything has to be on one line! nl2br() outputs as follows: Line one Line two And I want it to be as follows:

LEN in MySQL

In MySQL 5.1 LEN is called LENGTH, you use it exactly the same and pass in the link, so: LENGTH(link).

Remove all linebreaks in PHP

If you are having problems with a string that keeps adding a line break when output from PHP to HTML then the following code will work wonders for you! 1 2 $string_with_line_break = "blabla\nbla\r"; $string_without_line_break = trim(preg_replace( "/\s+/","",$string_with_line_break));

IE9 adding empty table cells in large table

Well as usual, we are once again fighting the woes of the beloved Internet Explorer, and it appears that Microsoft’s latest greatest new and improved browser rival to the market IE9 still has a whole bunch of irritations and retardation to worry about (saw that one coming). Today I was busy loading a massive amount of data into the body of a table using ajax and in Firefox and even Internet Explorer 8 it worked quite well, but Internet Explorer 9 decided that it would like to add some empty table cells randomly around my “massive table”, now of course I began by kicking something and cried a few times….

SELECT Duplicate Items Using SQL

If you have a table which has duplicate items in then you can use the below statement to select them. 1 2 3 SELECT * FROM tableName GROUP BY columnName HAVING ( COUNT( columnName) >1 )

MySQL Error: Can’t get hostname for your address

This error is thrown by MySQL because it tries to do a DNS lookup on the address connecting to the server and it fails due to the IP not being able to be resolved back to a name or some-other deeper DNS bust problem. The easiest way to fix this is to edit your my.ini or my.cnf (/etc/my.cnf) and in the [mysqld] section add the “skip-name-resolve” option. Near the top of the file this will look similar to the below:

Adobe drops flash for mobiles!

..and it’s official – so says Adobe’s blog (read it here). Adobe will be dropping support for Flash on future devices due to performance and a few other things, pushing the future more towards the HTML5 route, which isn’t neccessarily a bad thing, I just find it quite ironic and somewhat comical that they waited for Steve Jobs (Apple) to die before giving up their long time fight with Apple about Flash being or not being supported on mobile devices, namely the iPhone.

jQuery Colorbox not showing but background shows

I was using Colorbox for some lightbox type effects to show larger images when clicking on thumbnails, but for some reason the background was showing up but the pretty little box in the middle where the larger image is supposed to show never showed up, so this is how I made it finally appear after many failed attempts! 1 2 3 $("document").ready(function(){ $(".theCssClass a[rel='forGroupingRelCodeGoesHere']").colorbox({ onLoad: function () { $("#colorbox").show(); } }); }); So the “real” code that fixed it was the:

Get Value of Checkbox using jQuery

This one is quite an easy one, but a good one to mention non-the-less as it does definitely come in really handy. If you would like to get a checkbox’s value to send to the backend using jQuery you can always do the following: Firstly let’s draw some HTML to illustrate our example. 1 <input id="chkOurCheckbox" type="checkbox" /> Now we need some jQuery to do the real work. 1 var ourCheckboxValue = ($("#chkOurCheckbox:checked").

Everybody’s a web host

To cut straight to the point, “we’re living in the age where everybody’s a web host”. Whether it be small and quite pathetic or just another reseller of a larger service trying to get their cut of things, everybody truly is spamming the world with more and more web hosting packages. Just a few short years ago this problem didn’t exist and getting web hosting was quite expensive, unless you went with free services such as the good ol’ “geocities” and co, but then larger companies started up their shared hosting and reseller hosting packages which immediately led to everybody thinking all it took to become a web host was to get themselves a reseller account and start trading.

CSS Background Default

If you would like to override an already attached class containing a background then you can use the following: 1 background:transparent none 0 repeat !important;

Restrict PHPMyAdmin to IP Address

Would you like to restrict PHPMyAdmin from being accessible to the whole world? Firstly you need to SSH into your box if you are not on the local machine where PHPMyAdmin is installed along with Apache. 1 2 cd /etc/phpmyadmin/ vi apache.conf You will need to look for the following text “<Directory /usr/share/phpmyadmin>” and add in the below code: Remember that you need to press “i” in order to start editting text if using VI.

2014 – Commands out of sync; you can’t run this command now

#2014 – Commands out of sync; you can’t run this command now Now that can be a really annoying error! It happens when querying stored procedures in phpMyAdmin as mysqli seems to return 2 sets of results opposed to the usual 1 that mysql returns. You cannot call anything else until you have “flushed” or “cleared” the second result. You can try using COMMIT to resolve this.

Image corrupt or truncated: “unknown”

What is “Image corrupt or truncated: ” and what does it mean? This can often be seen while using Firebug in Firefox, but it actually has nothing to do with either and is just the error being reprinted in in firebug in most cases. It comes from an image that has had it’s extension changed from say .gif to .png or .png to .ico. Eventhough .png and .ico file types are very similar, they are not the same, so an error occurs.

WordPress wp-admin use different language for user

I needed to do some work on a WordPress site a little while ago and all the admins were French – and I cannot speak French other than the popular swear words – so had no idea what was going on in the /wp-admin/ so needed some way of enabling my newly created user account to be in english while letting everybody else use french as they had been up til that point.

Replace all spaces in HTML with   except in HTML tags using PHP

If you would like to replace all the spaces in an HTML string with so that you can render it out to the browser but you also want to retain current spaces in HTML tags you can use the below method: 1 2 3 4 $inputString = '<span class="color:red">1</span> 2 3'; $outputString = preg_replace_callback("#>[^<]+<#","relSpaces",$inputString); function relSpaces($match){return str_replace(" ","�",$match[0]);} This will replace: <span>1</span> 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3 ..to look like this: <span>1</span> 2������� 3

Using PHP to validate an IP address

You can use the PHP code below to check if an IP address is valid or not. 1 2 3 4 5 6 $ip = ""; //enter a valid or invalid ip address here if(filter_var($ip, FILTER_VALIDATE_IP)) { // The IP Address is valid } else { // The IP Address is not valid } Wasn’t that easy!?!