Generate HTML5

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:

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

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.


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.


 * 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.

6 Responses to “Generate HTML5

  1. julesj says:

    What about using the date constants available in PHP?
    I think both DATE_W3C and DATE_ISO8601 would do in this case (the W3C format seems logical from the HTML5 point of view, but Google demands the ISO version for microdata)

  2. Well spotted that man! I’d not come across these constants before, but I’ve just checked and DateTime::W3C works just fine. DateTime::ISO8601 didn’t work as it put a colon in the GMT-offset value – despite what the documentation says!

    So the first line inside the function could be replaced with this:

    	$strFormat = DateTime::W3C;

    Though of course with this knowledge you don’t need the function as it’s whole purpose was so you wouldn’t have to remember the formatting string which of course this constant negates…

  3. Aidan Lister says:

    Here’s a simpler, one line equivalent: echo date(DATE_W3C)

  4. baltech says:

    Thanks for this useful information…..:)

  5. Jesse Donat says:

    The date(DATE_W3C) trick is awesomely useful in more situations than this! Thank you, and the clever commenter!

  6. [...] <time datetime="2011-07-28T07:10:29+01:00">Too early on a Thursday morning after a broken night's sleep.</time> Can be easily created in php. See more at Pete Williams' blog, the creator of a great Amazon WordPress Plugin: HTML5 Time. [...]

Add a comment