Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to use the following code

the_time('d. F Y', '<p class="article-date">', '</p>');

to show the date of an article (post) wrapped within <p></p>. I also tried

 echo '<p class="article-date">' .  the_time('d. F Y') . '</p>';

but again the date isn't placed in those tags. What I'm doing wrong? I have a normal foreach and no endforeach and so on.

share|improve this question
    
I knew that it was wrong, but I wanted to show my efforts. I before had problems with the_date because it was shown only one time and not on each post. –  testing May 18 '12 at 16:21

2 Answers 2

up vote 5 down vote accepted
<p>
    <?php the_time() ?>
</p>

It helps with separation of concerns and increases readability of code as well as being consistent with WordPress coding standards and the default coding style.

The first method you attempted is completely invalid, the_time() only accepts 1 parameter, the date format. The second method has roots in validity, but the_time() itself is already doing an echo, so by calling it in the middle of the string, you're preempting the outer echo and probably getting the time and an empty p. If you want to use that method you can go with this:

echo '<p>' . get_the_time() . '</p>';

Since get_the_time() returns its value whereas the_time() echos it.

Edit

To use the $format parameter correctly:

<?php the_time( 'd. F Y' ) ?>

Note: for a publicly released Theme, it is recommended to use the user-defined date/time format:

<?php the_time( get_option( 'time_format' ) ); ?>

You can also pass the date format to the_time() if you need to output the date for each post in the loop. The the_date() function only outputs the date upon the first occurrence. So, if multiple posts have the same date, the date will only be output once. To get around this limitation, use the_time( $date_format ) instead:

<?php the_time( get_option( 'date_format' ) ); ?>
share|improve this answer
    
Didn't know that there were coding standards for WP. –  testing May 18 '12 at 16:19
    
Here's a link to the coding standards. –  m0r7if3r May 18 '12 at 16:49

about your first line of code:

the_time() only uses one parameter.

about your second line of code:

if you want to use 'the_time' in a string concatenation, use get_the_time() :

example:

echo '<p class="article-date">' . get_the_time('d. F Y') . '</p>';
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.