2

Before asking for help I would like to clarify that I don't have advanced skills on PHP. What I want to achieve is to display different messages for every period of time that I set (I need this to LOOP every day). After reading this -> http://codex.wordpress.org/Function_Reference/current_time I tried to check:

$my_time = current_time('mysql');
if ($my_time >= 14 && $my_time <= 16) {
    echo 'Do your homework';
} else if ($my_time > 14 && $my_time <= 18) {
    echo 'Clean up the house';
} else if ($my_time > 18 && $my_time <= 20) {
    echo 'Go Shopping';
  .
  .
  .
  .
  .
} else {
    echo 'Lets start over!'
}

Where 14, 16, 18, 20 is the time (24hour format). However this did not work. When I echo current_time('mysql'), it returns the date and the time so even if it could work, it wouldn't loop because of the date, right? Thanks in advance.

  • as you can see by clicking the link I attached, current_time works in wordpress so it is not off-topic – ArgGeo Apr 24 '13 at 13:11
5

You would just need to format the time that is returned by current_time() with the php date() function like this:

$my_time = date('G', current_time('timestamp'));

The param 'G' tells the function you just want to have the hour part (0 to 23) of the date. Have a look here: http://www.php.net/manual/en/function.date.php

3

You're right in that it is a WordPress function, and thus I removed my comment. However, this is still only related to WordPress, because of this being a WordPress function.

This has more to do with what a timestamp is and how you can work with it.

$my_time = current_time('timestamp');
$my_hour = date('G', $my_time);
if ($my_hour >= 14 && $my_hour <= 16)
    echo 'Do your homework';
elseif ($my_hour > 14 && $my_hour <= 18)
    echo 'Clean up the house';
elseif ($my_hour > 18 && $my_hour <= 20)
    echo 'Go Shopping';
else
    echo 'Lets start over!'

References:

  • PHP's date function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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