Code snippet : WordPress qTranslate – Getting current language

So I was working with the qTranslate plugin of WordPress’s and needed to select an image depending on language. qTranslate provides a utility function which returns the current language.

The function is called qtrans_getLanguage() and it works as follows:

You can find this function in qtranslate_utils.php file in qTranslate directory.

 

PHP: Get intersecting dates between 2 date ranges

Here is a small code snippet that will give you intersecting dates between 2 date ranges. Say, for example, you have 2 date ranges, 1-Jan-2011 to 31-Mar-2011 and 23-Feb-2011 to 4-May-2011.  This function will give you 23-Feb-2011 to 31-Mar-2011 as result.

Here is the code:

Above will show the following on browser :

If the 2 date ranges have any intersecting dates, an array will be returned which will have start and end elements that represent the beginning and end of intersecting range.  Set of dates given below will return false as there are no intersecting dates.

This will show

The code is self-explanatory. As you can see, first we have used php function strtotime to convert all dates to timestamps. Then we can compare them easily. This way you can use whatever(allowed) date format you wish.

PHP class for expanding and shortening URLs using Google URL shortener API

I have combined the 2 previous posts for expanding and shortening goo.gl URLs and have created a small class for it. Below is the download link for the class.

Download GAPIClass

Expanding a URL

Shorten A URL

As I have mentioned earlier that Google recommends an API key. The class has a flag variable called keyWarning. By default it is true and it will show the following warning message if you do not pass an API key while instantiating the class:

Currently you are not using an API key. It is recommended that you use one. Click here to learn more about the API key

To turn this notification off, just use this line:

Download GAPIClass

WordPress Internal Server Error 500 in wp-admin

If you are getting an internal server error 500 after installing wordpress while accessing admin, here is the fix.

Most likely it is caused by low memory limit on your server. To fix it, create a new file named php.ini in you wp-admin directory and put the following line in it.

memory_limit = 64M

Save the file, then reload the admin page and it will work fine.

PHP: Shorten URLs using Google URL shortener API

In past 2 posts you saw how short URLs can be expanded using the Google URL shortener API. This post shows how URLs can be shortened using this API.

API key is not mandatory for test purpose. Use this link to get an API key.

For shortening URLs, 2 points must be noted. First, a HTTP POST is required and the post data should be in JSON format. Here is the code:

Here is the JSON response from Google:

You can also read these: