WordPress date and time functionality for get_the_time()

WordPress and PHP Date Function

By Jonathan on

PHP has around 37 properties for its date function, using the wordpress get_the_time() function and the PHP date function it’s possible to get the date displayed for each post in a way that suits you and your theme. To change the output given by wordpress the get_the_time() function is called whilst passing the required arguments to it. These arguments are 1) the PHP date function to be displayed 2) the ID of the current post.

For example if we wanted to display the date of the post on 3 lines, each with different CSS styling we would need to call the get_time_function 3 times, and pass it different arguments each time.
Below is what we are going to achieve:

Here is the code:
{code type=php}





Lets start from the top.

Firstly to have each part of the dates on a new line, put them inside <p> tags with a class which defines the styling of each part. IE, class ‘date-day’ is font size 60 and class ‘date-month’ is font size 28.

Inside of the <p> tag is the get_the_time() function, within the call function the arguments need to be included to define which part of the date to acquire. The get_time_function() accepts two arguments firstly is the formatting for the date or time. For this, the PHP date/time is case-sensitive, so remember this! The formatting needs to also be placed first, and within apostrophes. For the Date, the letter ‘d’ is used, note this is lower-case. This will give us the date formatted ’01 to 31′, if we wanted to loose the ’0′ from in front of the number the letter ‘j’ would be used. (See bottom of page for common formatting)

The second instance of the function is wrapped in the strtoupper() function, this function changes any string to uppercase, which will suit our formatting nicely.

That is quite simply it! Below is a list of some common formatting for dates and times and the code you’ll need to put them into your wordpress page.

[exec]echo get_the_time(‘d M Y’, $post->ID);[/exec] {code type=php}

[exec]echo get_the_time(‘l jS F Y’, $post->ID);[/exec] {code type=php}

[exec]echo get_the_time(‘d/m/Y’, $post->ID); echo ‘ at ‘; echo get_the_time(‘H:i’, $post->ID);[/exec] {code type=php}
ID); echo ‘at’; echo get_the_time(‘H:i’, $post->ID);?>

[exec]echo get_the_time(‘j/M/y’, $post->ID); echo ‘ at ‘; echo get_the_time(‘g:i.sA’, $post->ID);[/exec] {code type=php}
ID); echo ‘ at ‘; echo get_the_time(‘g:i.sA’, $post->ID);?>

For a full reference see the php date function page

About Jonathan

Jonathan likes playing with code, cars and ermm... digital things

Related Posts
Comments (1)