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