Echo Messages By Checking current_time()

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.

Related posts

Leave a Reply

2 comments

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