PHP Tutorial


PHP 7 Date And Time

Author:- w3alert.com
Updated Date:-01-02-2020

PHP Date and Time 

The date and time functions are most commonly used in PHP. Which are used to convert the date and time format and one format to another format and PHP has some built-in functions to perform these tasks. With the help of which date and time are made understandable by humans.

What is timestamp? And why do we need the date () function?

The computer stores the date and time in a format called UNIX timestamp, which measures time as the number of seconds since the starting of the UNIX era [ Midnight Greenwich Mean Time on January 1, 1970, to January 1, 1970, 00:00: 00 GMT ]
Since this is an abstract format for humans to read, PHP changes timestamp to a format that is readable and more understandable to humans.

PHP date() function

The PHP date() function is a pre-defined function that is used to convert date and time formats to human-readable formats. 

Syntax:

    date( format, timestamp);

There are two parameters used in the PHP date() function.

Format:    In the PHP date () function, the format parameter executes the date and time format.

Timestamp:    This is an optional parameter if the timestamp is not set then the current date and time will be used.

Formatting options in PHP date() function

The format parameter of the PHP date() function is a string in which different characters are used to execute the date in different formats.

The following date related formatting characters are shown here:

  • d    -    It signifies the day of the month in two-digit ( 01 to 31 ).
  • D    -    It signifies the day of the week in the text ( Mon to Sun ).
  • m    -   It signifies the month in number ( 01 to 12 ).
  • M    -   It signifies the month in the text ( Jan to Dec )
  • y    -    It signifies the year in two-digit (06 to 19 ).
  • Y    -    It signifies the year in four-digit ( 2006 to 2019 ).
  • You can create a visual format by inserting parts of the date with hyphens (-), dots (.), slash (/), Space, etc.

Example: Below program explains the usage of the PHP date() function.

Let's create a program to convert date and time format.

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    /* Date formates */

    echo date( "d" ) . "<br>";        // the day of the month in two digit.

    echo date( "D" ) . "<br>";        // the day of th week in text.

    echo date( "m" ) . "<br>";        // the month in number.

    echo date( "M" ) . "<br>";        // the month in text.

    echo date( "y" ) . "<br>";        // the year in two digit.

    echo date( "Y" ) . "<br>";        // the year in four digit.

    echo date( "d/m/y" ) . "<br>";

    echo date( "d M y" ) . "<br>";

    echo date( "d-m-Y" ) . "<br>";

    echo date( "d.M.Y.D" ) . "<br>";

    ?>

    </body>
    </html>

The following characters can be used with date() function to format the time. they are shown here:

  • h    -   It signifies an hour in 12-hour format (01 to 12).
  • H    -  It signifies an hour in 24-hour format (00 to 23).
  • i    -    It signifies minutes (00 to 59).
  • s    -   It signifies seconds (00 to 59).
  • a    -   It signifies lowercase ante meridian and post meridian (am or pm).
  • A    -   It signifies uppercase ante meridian and post meridian (AM or PM).

Example:

Let's create a program to get time with the date () function of PHP.

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    /*Get Date and Time*/

    echo date("d M Y h : i : s a") . "<br>";

    echo date("d M Y H : i : s A");

    ?>

    </body>
    </html>

PHP time() function

The time() function is a built-in function in PHP which used to get the current time as a Unix timestamp ( the number of seconds since the starting of the UNIX era: 1 January 1970 

Syntax:

    time( );

Note: PHP time() function does not support any parameter.

The following time-related formatting characters are shown here:

  • h    -  It signifies an hour in 12-hour format (01 to 12).
  • H    -  It signifies an hour in 24-hour format (00 to 23).
  • i    -    It signifies minutes (00 to 59).
  • s    -   It signifies seconds (00 to 59).
  • a    -   It signifies lowercase ante meridian and post meridian (am or pm).
  • A    -   It signifies uppercase ante meridian and post meridian (AM or PM).

Example: 

Let's create a program to get the current time with the PHP time() function.

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    /*Get time*/

    echo date("d M Y H : i : s A", time());

    ?>

    </body>
    </html>