Get hour and year from current date and time in PHP

Description

The following code shows how to get hour and year from current date and time.

Example


//ww w. ja  v a 2  s  .c o m
<!DOCTYPE html>
<html>
  <body>
    <?php
    
    $hour = date( "G" );
    $year = date( "Y" );
    
    if ( $hour >= 5 && $hour < 12 ) {
      echo "<h1>Good morning!</h1>";
    } elseif ( $hour >= 12 && $hour < 18 ) {
      echo "<h1>Good afternoon!</h1>";
    } elseif ( $hour >= 18 && $hour < 22 ) {
      echo "<h1>Good evening!</h1>";
    } else {
      echo "<h1>Good night!</h1>";
    }
    
    $leapYear = false;
    
    if ( ( ( $year % 4 == 0 ) && ( $year % 100 != 0 ) ) || ( $year % 400 == 0 ) )
      $leapYear = true;
    
    echo "<p>Did you know that $year is" . ( $leapYear ? "" : " not" ) . " a leap year?</p>";
    
    ?>
  </body>
</html>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone