Code Posts:

Generate HTML5

Thursday, September 16th, 2010

Having just finished reading Introducing HTML5 I’m rather excited about all the new HTML5 tags and can’t wait to get using them!

One of the new semantic tags is the <time> tag which is used to mark up dates and times so they are readable to both humans and to machines. It does this by including a datetime attribute which contains the relevent date in a standardised format.

For instance:

<p>
   This post was written on <time datetime="2010-09-16T21:28:29+01:00">Thursday evening</time>
<p>

As you can see, the machine readable format is “YYYY-MM-DD” with a “T” to separate the time in the 24-hour format HH:MM. It ends with your timezone’s offset from UTC/GMT.

UPDATE

Ignore everything that follows! As clever commenters pointed out, there is actually a PHP constant for this format – DATE_W3C. All you need to get the current time is…

<?php echo date(DATE_W3C); ?>

All that follows, from the original post, is left for reference only…

To make this easier to generate when dealing with dates in PHP, I have written a function to do this for me, which is below. Feel free to use it and let me know of any suggestions for improvement.

To use the function, pass in a unix timestamp or leave blank to use the current time.

<?php

/** 
 * formats the date passed into format required by 'datetime' attribute of 

Example: view current time in datetime format.

Buy domain names for just $13/year.

Display HTML OL/UL list as comma-separated sentence with CSS

Wednesday, April 21st, 2010

On a site I’m currently working on, it seemed best to display an OL as a list in sentence form.

For example:

<ul class="commaList">
	<li>one</li>
	<li>two</li>
	<li>three</li>
	<li>four</li>
</ul>

Would display as: One, two, three and four.

In case this CSS is of any use to anyone else, I’ve published it as a snippet below.

Please note this uses pseudo-selectors and so will not work in IE, but will gracefully degrade.

/* display ordered list as comma list */
.commaList li:first-child {
	text-transform: capitalize;
}

.commaList li:after{
	content: ',';
	padding-right: 5px;
}

.commaList li:nth-last-child(2):after {
	content: '';
	padding-right: 5px;
}

.commaList li:last-child:before {
	content: 'and ';
	padding-right: 0;
}

.commaList li:last-child:after {
	content: '.';
	clear: both;
}

/* this prevents single item lists from containing commas and 'ands' */
.commaList li:only-of-type:before, .commaList li:only-of-type:after {
	content: '';
	padding: 0;
}

Amazon Link Localizer Greasemonkey Script

Monday, September 28th, 2009

This Greasemonkey script will convert links to international Amazon sites into links to your local Amazon site.
So if I link to a product on Amazon.co.uk, you will get a link to that same product on Amazon.com instead.

Greasemonkey is an add-on for Firefox that allows you to run user-generated scripts on the websites you visit. If you haven’t got it yet, download it now, as you’ll need it to install the below script.

Once you’ve installed Greasemonkey, simply click the below link to install the script.

As you’re visiting from US, you’re probably looking for the Amazon.com Script, right?

Download: Amazon.com Link Localiser (or see all options)

Updates

Follow me on Twitter to be sure you hear about new updates.

Amazon Affiliate Link Localizer WordPress Plugin

Friday, September 25th, 2009

Download

Current Version: Amazon Affiliate Link Localizer 1.8.3
Last Updated: 19/03/2013

Description

This plugin not only automatically changes any Amazon link on your site to use your affiliate ID, but it also changes the link to point to the user’s local Amazon store.

So if your visitor is visiting from the UK they’ll get a link to Amazon.co.uk, if they’re visiting from the US they’ll get a link to the same product on Amazon.com. Initially, the script looks for a product with the same product ID as the one in your link, but if it can’t find one on the user’s local store, it will link to a search results page searching for the name of your product. This means that it will still be found even if international versions have different product IDs, and if it’s not stocked at all, your user will be shown similar alternatives.

All you have to do is provide all your affiliate IDs!

Support

If you are having any problems with the script, please read the FAQ which solves most common enquiries.

Non-Wordpress version

If you want to achieve the same result on a non-Wordpress site, you can simply install the pure JavaScript version.

See also: Greasemonkey Script

As an Amazon user, you may also be interested in my Amazon Link Localizer Greasemonkey Script which automatically converts links to international Amazon sites into links to your local Amazon site.

So if I link to a product on Amazon.co.uk, and you are a US visitor, you will get a link to that same product on Amazon.com instead.

Updates

Follow me on Twitter to be sure you hear about new updates.

Donations

If you find this plugin helpful and it’s increasing your sales. please consider donating just $1 or more to say thanks for the many, many hours I’ve spent on it. I’d really appreciate it!

Disclosure

If you do not insert your own affiliate IDs the script will use mine. Hopefully this will help fund my book addiction!

Amazon Associate Link Localiser

Tuesday, July 14th, 2009

I’ve been a member of Amazon’s Associate program for a number of years and have posted affiliate links to books discussed on this very blog. The program means that by putting my affiliate ID into any links to Amazon products, I receive a small referral fee.

I’ve always linked to Amazon.co.uk as that’s my local Amazon store, but of course that’s not necessarily true for my visitors – or yours…

To solve this problem, I’ve written some JavaScript and a little bit of PHP which will convert any Amazon links on your page into affiliated links to your visitor’s local Amazon store. So US visitors will get a .com link, whilst UK visitors will get a .co.uk link to the same product.

Download

Setup

Edit amazon-localiser.js to insert your regional Associate IDs into the array at the top, for example:

var arrAffiliates = {
	'co.uk' : 'petewill-19',
	'com'	: 'petewill-20',
	'de'	: 'petewill05-21',
	'fr'	: 'petewill-21',
	'ca'	: 'petewill00-20',
	'jp'	: '',
	'it'	: petewill04-21',
	'cn'	: petewill-23',
	'es'	: petewill0d4-21'
};

If you do not have an ID for each country, you can just leave them blank, as above for .jp.

In most cases, you will also need to set the path to amazon-localiser.php, for example:

var strUrlAjax = '/inc/amazon-localiser.php';

Upload and Embed

As well as linking to amazon-localiser.js you need to link to the Google JS API file (5 kb) or the script will not work. Insert the following code just before your </body> tag:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="amazon-localiser.js"></script>

Upload your modified amazon-localiser.js file, and make sure the above link points to the right location.

Usage

That’s it – job done! You can now just link to any product on any Amazon site with a normal, unaffiliated link and your users will get one affiliated for their country. Initially, the script looks for a product with the same product ID as the one in your link, but if it can’t find one on the user’s local store, it will link to a search results page searching for the name of your product. This means that it will still be found even if international versions have different product IDs, and if it’s not stocked at all, your user will be shown similar alternatives.

Example

The script is running on this page, so hopefully this example should be affiliated specially for you!

Updates

Follow me on Twitter to be sure you hear about new updates.

Donations

If you find this plugin helpful and it’s increasing your sales. please consider donating just $1 or more to say thanks for the many, many hours I’ve spent on it. I’d really appreciate it!

Disclosure

If you do not insert your own affiliate IDs the script will use mine. Hopefully this will help fund my book addiction!