Course Banner


Date And Time

Date is one of the frequently used things in any web applications. Whether you want to keep record of the order date or you want to know which date did you book your hotel last time, you need date function for date manipulation.

PHP provides function called date() which converts machine timestamp date into human readable format.

Why do we need the date() function?

The reason we require date function is that the computer stores dates and times in a format called UNIX Timestamp, which calculates time as number of seconds since the beginning of the Unix epoch which starts from midnight Greenwich Mean Time on January 1, 1970 i.e. January 1, 1970 00:00:00 GMT .

Since reading this format is not possible for human being, PHP converts a timestamp to a format which is human readable format.

Syntax:

date(format, timestamp)

The first argument is the date format and the second argument is optional timestamp. If not provided then current data and time is taken.

<?php
  1.       echo"Today's date is :";
  2. $today = date("d/m/Y");
  3.       echo$today;
  4. ?>

The output :


The required format parameter of the date() function specifies how to format the date (or time).

There are several date formats which you can find in PHP’s official website www. php.net/manual/en/function.date.php . Below are few of the common date functions.

d – Shows the day of the date (01 to 31)

m –Shows the month of the date (01 to 12)

Y – Shows the year (in four digits)

l (lowercase 'L') - Represents the day of the week

Example

<?php
  1.       echo"Today is " . date("Y/m/d") . "
    "
    ;
  2.       echo"Today is " . date("Y.m.d") . "
    "
    ;
  3.       echo"Today is " . date("Y-m-d") . "
    "
    ;
  4.       echo"Today is " . date("l");
  5. ?>

Output :


Time Zone Setup

If the server location is at different country/place then it’s possible that you may not get correct time because of being in different time-zone. If you want to get/display correct time zone as per the location, then you can set time-zone and it will display correct time.

For example, if you want to display correct time of Kolkata, then you need to write 'Asia/Kolkata'.

The following characters can be used along with date() function to format the time string:

h – Displays hour in 12-hour format with leading zeros (01 to 12).

H – Displays hour in in 24-hour format with leading zeros (00 to 23).

i – Displays minutes with leading zeros (00 to 59).

s – Displays seconds with leading zeros (00 to 59).

a – Displays lowercase ante meridian and post meridian (am or pm).

A – Displays uppercase ante meridian and post meridian (AM or PM).

<?php
  1.       echodate("h:i:s") . "n";
  2.       echodate("M,d,Y h:i:s A") . "n";
  3.       echodate("h:i a");
  4. ?>

PHP data mktime

The mktime() function is used to create the timestamp for a specific date and time.

If no date and time is provided, the timestamp for the current date and time is returned.

Syntax:

mktime(hour, minute, second, month, day, year)

<?php
  1.       echomktime(23, 21, 50, 06, 21, 2018);
  2. ?>

In the above example, first parameter is second, next minute then hour, then month and data and then year.

Output: