PHP date function – output a friendly UK style date with ordinal date suffix and ‘hrs’ suffix on time

14th April, 2012 – 5:53 pm

This post is more to remind me in the future but perhaps someone else will find it useful?

I like the UK date format – I like to fight the Americanism of not using the ordinal suffix and I also like ‘hrs’ on 24-hour clock times.

Sun 18th Mar 19:00hrs

Here is the date function for it…

date(‘D jS M H:i\h\r\s’, $mydate);

If $mydate is omitted you’ll get the time now.

Warning – if you use double quotes you won’t get ‘hrs’ you’ll get ‘h s’ as the \r will be converted as return char!!

Note how I escape chars that aren’t even PHP date() control chars, this is future proofing a new control char being added – they only added microseconds in 5.2.2!

Post a Comment