Quickest way to get timestamp of a time in a particular timezone in PHP

Playing with timezone conversions can be crazy, it still confuses me every time and I have to wait a minute to run some test code to figure out what's it that I am trying to do here.

Anyway, the quickest way I have to get timestamp of a time in particular time zone is

DateTime and DateTimeZone classes are what you need to know when dealing with timezones, ditch any other method of doing conversions in PHP. Seriously, there are no better ways of doing it.

And just a tip, if you already don't know, timestamp from different timezones for a single point of time is same. It doesn't depend on the timezone. Did you know? πŸ™‚

Let me know in the comments if you have a question.

Get WordPress images from a different site

Developers usually work on their local installs before pushing any changes to the live or production server and that's how a developer's workflow should be.

In order to setup a local or dev install on which a developer can work or test, they need to pull down the codebase and database dump to exactly mirror the site but they also need to download the media folder to make sure all images show up fine, which can have a bit of its own issues.

Codebase is usually under version control, so that's easy to pull and push changes. Database dump is a single file to download. But media folder can be very large sometimes and not possible to download every time you want to sync up with the files on live site. A super easy solution, that I use, is to setup a redirection rule in your local webserver to redirect all media requests to the live site and then you don't need to sync your media files at all.

Here is a single line, that can be added in your WordPress .htaccess file (if you are using apache, for nginx or any other webserver, idea stays the same, syntax will vary ofcourse):

RedirectMatch 302 ^/wp-content/uploads/(.*)$ http://mylivesite.com/wp-content/uploads/$1

Voila! Now you have all images showing up from live site directly πŸ™‚

P.S. - This doesn't work if you are offline for obvious reasons.

WordPress plugin Facebook Like Thumbnail Updates

I have updated my WordPress plugin to fix Facebook Like Thumbnails to version 0.2 which I am sure will make the existing users happy! πŸ™‚

Some of the users who quickly upgraded might be facing issues because of a silly mistake on my part. If the plugin has disappeared from the plugins listing, then you would have to delete the folder "facebook-like-thumbnail" folder in your plugins folder (/wp-content/plugins/) and then reinstall the plugin by typing "Facebook Like Thumbnail" in Add New Plugins screen. I apologise for the issue, please excuse me, I am not used to pushing updates to public repositories.

The plugin now features an options pages which can be found under Settings > Facebook Like Thumbnail. You can specify your default thumbnail image on that page, no more code edits are required. This was a necessary move as the users would have to edit the plugin to specify their default image after every upgrade.

The plugin now supports featured thumbnails and NextGEN galleries (other than the slideshow ones).

The plugin picks the thumbnail in the following order (whichever is found first)

  • If its a post or page
    • Featured Thumbnail
    • First Image in the post
    • Default
  • If its a Front Page or Search Page
    • Default
  • Anything else
    • First image in the first post of the loop
    • Default

Facebook Like Thumbnail

Author: Ashfame, version: 0.4, updated: December 7, 2016,
Requires WP version: 3.1 or higher, tested up to: 4.7.7.
Download (85 049 hits) (5 votes)

Adding the author avatar on author pages to be used as thumbnail and ignoring smilies as a possible match for thumbnail are on my To-do lists. If you have any suggestions, comments section is all yours.

Also the plugin can be totally uninstalled by using the delete option from WordPress dashboard, it won't leave anything behind in the database.

I have tried to keep the plugin as fast as it can be. If any developer can provide any suggestions/improvements, I would love that. If you have any questions or facing any issues because of the early upgrade or need any help, give me a shout in the comments.

Which one to use WP_Query vs query_posts() vs get_posts()?

There are several instances where we want to display content other than what WordPress displays at a particular page. There are three methods which you can use, but each of them is meant for a specific purpose, otherwise why would they have existed? Makes sense? Good!

Some tutorials use one, some other and many of them are in fact incorrect because they might seem to get the job done but in certain cases they cause side effects, such as modified global variables on which other functionality may rely.

The fight is in between using WP_Query or query_posts or get_posts. Rarst created a great chart to explain what is going on in the three approaches to make it easy to understand the working. which I totally recommend checking out.

wordpress query

Rarst explained the difference quite precisely as follows:

  • query_posts() should be used in one and only case if you need to modify main query of page. It sets a lot of global variables and will lead to obscure and horrible bugs if used in any other place and for any other purpose;
  • get_posts() is very similar in mechanics and accepts same arguments, but returns array of posts, doesn't modify global variables and is safe to use anywhere;
  • WP_Query class power both behind the scenes, but you can also create and work with own object of it. Bit more complex, less restrictions, also safe to use anywhere.
I want to stress that query_post() wrongfully overused in many tutorials around. It is one of the most widespread bad practices.

Next time you need to make WordPress to show content other than what it does by default, take care to choose the right method. If there is something that you don't understand in the chart, leave a comment here and I will happily explain. πŸ™‚

Using Ubuntu Notification System – NotifyOSD

Ubuntu features a notification system, where you can see a message notifying you about some particular event. Rhythmbox uses it to show the next track when a track ends, Filezilla shows a notification that file transfers are completed when its window is not in focus (Very handy!) and so on several applications can use it to notify users in the same way (keeping the UI consistent, which is a good design principle).

Rhythmbox Song Notification

rhythmbox notify

Filezilla Transfer Complete Notification

filezilla notify

We too can use this easily in our shell scripts. It supports more functionality if you are working in Python or C (Read Ubuntu NotifyOSD) but for shell scripts it does the pretty fine job too, if you just want to make Ubuntu work for you.

Using NotifyOSD in shell scripts

Make sure you have libnotify-bin installed, if not, just install it by typing the following command in a terminal:

sudo apt-get install libnotify-bin

notify-send - Program to send desktop notifications

With notify-send you can sends desktop notifications to the user via a notification daemon from the command line. These notifications can be used to inform the user about an event or display some form of information without getting in the user’s way.

Check out the man page for it, to see the options it has to offer - man notify-send

Enter this in a terminal, notify-send "Hello" "this is just a test" and you will see the notification appearing up on your desktop.I usually collect icons that I would like to use and then use them in notification bubbles in the following manner:

In terminal, you can use it like

notify-send -t 2000 -i /home/ashfame/Dropbox/Ubuntu/icons/console.png "Hello Ashfame" "This is your computer, lets start with work"

In a shell script, use it like (take care of the screen where you want to display the message - I am on a dual screen setup)

DISPLAY=:0 notify-send -t 2000 -i /home/ashfame/Dropbox/Ubuntu/icons/console.png "Hello Ashfame" "This is your computer, lets start with work"

notify example

Time parameter (-t) lets you specify the time in miliseconds after which the notification will fade away. Its affected with a bug right now (will work on the default timeout even if you set it, but will be fixed in upcoming releases anyway, so better use it).

Icon parameter (-i) is used to specify the icon which is to be used in the notification.

You can also use it to notify you when a certain command has completed in the terminal, like when you were compiling some code or anything which takes a good amount of time so that you can get to know the moment it is ready. As an example, you can use it like

make && notify-send "DONE"

Making practical use of NotifyOSD using notify-send

You can create shell scripts for additional functionality you want, right? Just use notify-send where you want you to be informed or alerted.

Here are the examples, where I use them:

  1. WordPress new version alert
  2. Alert if my site is down
  3. Health check - Tells me to take breaks, sleep at night and if I resist, lock the screen
  4. Pomodoro technique for quantizing time for increasing productivity
  5. Random fun facts from randomfunfacts.com

You can build a lot more useful stuff as per your needs. Share your ideas in the comments and we can discuss πŸ™‚

Deal with Blog Scrappers getting indexed quicker than the original site

Although its not something that one should be worried about as such things often happen, and its actually a sign that you are growing. I would suggest you to just keep going on with the quality content on your site and not to worry about them scrapping your articles. Google does a pretty good job in killing spam blogs. They generally gain traction for a month or so and then they are completely gone.

But sometimes it might happen that the spam blog site might be getting indexed quicker than the original site when your original site is pretty much new, so it can be a temporary hold for your organic traffic growth. In such case, we can deal with them by delaying the feeds for a certain amount of time as all these scrappers work by pulling articles from your feeds and then publishing your articles on their site.

Delay publishing of WordPress Feeds:

Here is the snippet with you can delay your feeds for (lets say 15 minutes):

 * Publish the content in the feed 15 minutes later
 * $where ist default-var in WordPress (wp-includes/query.php)
 * This function an a SQL-syntax
function publish_later_on_feed($where)
    global $wpdb;
    if ( is_feed() )
        // timestamp in WP-format
        $now = gmdate('Y-m-d H:i:s');
        // value for wait; + device
        $wait = '15'; // integer
        // http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
        $device = 'MINUTE'; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
        // add SQL-sytax to default $where
        $where .= " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";
    return $where;
add_filter('posts_where', 'publish_later_on_feed');

This will delay the feeds for 15 minutes (Line 14 in the code) before any new article appears in it. This is a very good approach in killing those automated blogs. But sometimes it can be the case, that they are not automated. Its humans manually copy-pasting the articles from various sources. In such a case, what you can do is to make your blog ping the crawl bots so that your chances of getting indexed first is maximised.

Checklist for fast indexing:

  • Submit a Sitemap to Google Webmasters.
  • Use PushPress and RSS Cloud WordPress plugin.
  • Use WordPress option to ping pinging service and add several multiple pinging service there (less effective now but doing it won't harm)
  • Delay your feeds for a few minutes (Scrappers won't be manually monitoring your site every minute)

Hope that helps you defeat those blood sucking scrappers. If you have any questions or tip, feel free to leave it in the comments below.

Last.fm free Internet Radio hack

Last.fm is a great service which scrobbles (track) what music you listen to and then recommend you new music based on what you like to listen. You can totally keep your music database online and discover new music that you will actually like. It works great. It offers an Internet radio service which is paid for countries other than US / UK / Germany. So, if you are from any other country, the trick is to use a country based proxy for listening to your Last.fm radio for free.

For the purpose of this post, I assume that you are ready running the Last.fm scrobbler (tool which sends your music statistics to Last.fm). In the menu, select Tools, and then options.


In options, select Connection from the left. Click on Manual proxy settings and fill in the IP address and port of the proxy server.


However note that only US / UK / Germany based proxy will work. Learn how to find a good and fast proxy server IP address & port.

Now you can enjoy free Internet radio without subscribing to their service. But I would suggest if you really enjoy their service, you should subscribe (its only 3USD/month). But I guess their policy of making users pay who are outside of US, UK & Germany sort of turn people off to subscribe. They shouldn't discriminate on the basis of geographical locations. After all, its love for music.

If you have any questions, leave a comment below.