Home > DeveloperSection > Articles > PHP Date() Function

PHP Date() Function


PHP PHP 
Ratings:
0 Comment(s)
 3289  View(s)
Rate this:

PHP Date() Function

PHP date () function is used for retrieving  the current date and time from the server, you can used ‘date’ function to set the format of date and time with many different ways.

The PHP date () function formats a timestamp to a more readable date and time (where timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred).

The basic syntax of ‘date’ function is given follows.

Syntax:

date (Format, Timestamp) ;

Where parameters are,

·         Format – Specifies the format of timestamp, it is required parameter in date function.

·         Timestamp – It is an optional parameter which specifies the timestamp, default is date and time.

There are lots of characters used in PHP to modify format of date function. Here I am described some characters which is widely used in formatting of date function. Let’s see the brief description about these.

·         ‘d’ : Character ‘d’ represents the day of the month.

·         ‘m’ : Character ‘m’ represents the month of year.

·         ‘y’ : Character ‘y’ represents the year.

 Let’s we have an example, how to format date function in PHP.

Example:

<html>

       <head>

              <title> Date Function </title>

       </head>

      

       <body>

              <?php

                     // Set defaulttimezoneused by all date time function

                        date_default_timezone_set ("Asia/Calcutta");

 

                      // print date in differentformats

                         echo "date format is y-m-d  : ".date("y-m-d")."</br>";

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

                         echo "date format is y/m/d  : ".date("y/m/d");

              ?>

       </body>

</html>

Output:

PHP Date() Function

Adding Timestamp with date( ) function:

The optional timestamp parameter in the date () function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used. “The ‘mktime()’ function returns the Unix timestamp for a date”. PHP time stamp is a numeric value in seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Let’s we have an example, how to add timestamp with date function.

Syntax:

mktime( Hour, Minute, Second, Month ,Day, Year, is_dst);

Basically, mktime() function is useful for doing date arithmetic and validation.

Where all parameters are defined as:

·         Hour:  It is optional parameter which specifies the hour.

·         Minute: It is optional parameter which specifies the minute.

·         Second: It is optional parameter which specifies the second.

·         Month: It is optional parameter which specifies the month of year.

·         Day: It is optional parameter which specifies the day of month.

·         Year: It is optional parameter which specifies the current year.

·         is_dst:  It is also optional parameter. This parameter can be set to 1 if the time is during daylight savings time (DST), 0 if it is not, or -1 (the default) if it is unknown whether the time is within daylight savings time or not.

Note: This parameter became deprecated in PHP 5. The new time zone handling features should be used instead.

Example:

<html>

    <head>

      

        <title>Date Timestamp</title>

    </head>

    <body>

        <?php

        // put yourcode here

        date_default_timezone_set ("Asia/Calcutta");

        $tomorrow = mktime (0,0,0, date("m"),date("d")+1,date("Y"));

        echo "Tomorrow date is : ".date("Y/m/d",$tomorrow);

        ?>

    </body>

</html>

Output:

PHP Date() Function


Don't want to miss updates? Please click the below button!

Follow MindStick