Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not use the the_time() function in an if statement using WordPress?

Tags:

php

wordpress

I am trying to save the current month as a variable and then based on the value, I want it to display the full name of the month. When I use the_time() in a function, it automatically echos out it out, which I do not want.

Here's the code:

$month = the_time('M');
if($month == 'Aug') { echo 'August'; }

How can I save the value of the_time('M') in a variable to use in an if statement without it echoing out the value?

btw, I know I can use the_time() function to echo out the full name of the month, but I am using this as a test. Based on the what the_time() is, I want to display a different image that corresponds to the month.

like image 258
zeckdude Avatar asked Jan 21 '26 01:01

zeckdude


2 Answers

How can I save the value of the_time('M') in a variable to use in an if statement without it echoing out the value?

Use get_the_time().

Every the_... function in Wordpress has a get_the_... equivalent that will return the value instead of echoing it.

like image 64
Pekka Avatar answered Jan 23 '26 15:01

Pekka


Instead of the_time(), use the get_the_time().

like image 24
xdazz Avatar answered Jan 23 '26 15:01

xdazz