PHP Date and Time


December 17, 2021, Learn eTutorial
1377

In this PHP tutorial, you will learn all about the Date and Time functions used in PHP. We will discuss in detail the Date and Time functions and their uses and formatting.

What is the use of the date() function in PHP?

The PHP date() function is a built-in function that is used to work with date data types. The PHP date() function helps to convert a date or time to a human-readable format. The date() function's mandatory format argument defines how to format the date (or time).
Here are some examples of characters that are frequently used for dates:

  •    d - Represents the month's day (01 to 31)
  •    m - Denotes a month (01 to 12)
  •    Y - Denotes a year (in four digits)
  •    l ('L' in lowercase) - Denotes the day of the week

Other characters, such as "/", ".", or "-" can be placed between the characters to provide formatting.
The date() function in PHP will only return the current date/time as of the server.

Syntax

date (format, timestamp(optional));

Example

echo "Today is " . date("Y/m/d") . "\n";
echo "Today is " . date("Y.m.d") . "\n";
echo "Today is " . date("Y-m-d") . "\n";
echo "Today is " . date("l");

Output:

Today is 2021/11/27
Today is 2021.11.27
Today is 2021-11-27
Today is Saturday

What is a TimeStamp in PHP?

The date() method generates a formatted string that represents a date. With a string parameter that you must supply to date(), you may exert a great deal of control over the format that it returns. The date() function accepts a timestamp as an optional parameter; if not specified, the current date and time are used. Any additional data in the format string given to date() will be included in the return value. In PHP, a timestamp is a numeric number in seconds between the current time and the value as of January 1st, 1970 00:00:00 Greenwich Mean Time (GMT). The time() function returns a value based on the default time zone. The php.ini file specifies the default time zone. The date_default_timezone_set method can also be used to set it programmatically.

Syntax

time();

Example

echo time ();

Output:

1637997813

To extract the list of time zones identifiers currently accessible 

We use the method “DateTimeZone::listIdentifiers()” to extract the available time zones.
Example

 
$timezones = DateTimeZone::listIdentifiers();
foreach($timezones as $key => $zones){
    echo "$key $zones \n";
}

Output:




PHP - Form

There are 424 time zones available and in the above example, we can see that we have accessed the timezones and printed the value using the for-each loop.

How to set the Timezone Programmatically in PHP?

To set our current date and time we have to use the method date_default_timezone_set() in its parameter we pass the required timezone.
Example

// to know the default timezone
echo "The time in " . date_default_timezone_get() . " is " . date("H:i:s");
echo "\n";
// to set the timezone
date_default_timezone_set("Asia/Calcutta");
echo "The time in " . date_default_timezone_get() . " is " . date("h:i:s a");

Output:

The time in UTC is 07:53:01
The time in Asia/Calcutta is 01:23:01 pm

Here are some examples of characters that are frequently used for date-time formatting:

Characters Description

a

It is used to represent ‘am’ or ‘pm’ in lowercase letters

A

It is used to represent ‘AM’ or ‘PM’ in uppercase letters

d

It is used to represent the day of the month with a leading 0

D

It is used to represent the day of the week in three letters (Mon)

F

It is used to represent the month

h

It is used to represent the time in 12-hour format with a leading 0 in

H

It is used to represent the time in 24-hour format with a leading 0 in

g

It is used to represent the time in 12-hour format without leading 0 in

G

It is used to represent the time in 24-hour format without leading 0 in

i

It is used to represent the minute (0-59)

j

It is used to represent the day of the month without leading 0

l

It is used to represent the day of the week

L

It is used to represent the leap year (it will return 1 if true or 0 if false)

m

It is used to represent the month in digits with a leading 0

M

It is used to represent the month in three letters (Feb)

n

It is used to represent the month in digits without leading 0 

s

It is used to represent the seconds of the hour

U

It is used to represent the timestamp

y

It is used to represent the year in two digits

Y

It is used to represent the year in four digits

z

It is used to represent the day of the year

Z

It is used to represent the offset in seconds from GMT

 

The most commonly used date and time function in PHP

  1.  date_add() function: PHP date_add() function is used to return the date from the given date and the given interval.

    Syntax

    date_add(object, interval);
    
    

    Example

    $date=date_create("2021-11-27");
    date_add($date,date_interval_create_from_date_string("20 days"));
    echo date_format($date,"Y-m-d");
    
    

    Output:

    2021-12-17
    
  2.  date_format() function: PHP date_format() function is used to return a date formatted in the format given.

    Syntax

    date_format(object, format);
    
    

    Example

    $date=date_create("2021-11-27");
    echo date_format($date,"Y/m/d");
    
    

    Output:

    2021/11/27
    
  3.  checkdate() function: PHP checkdate() function is used to return that the entered date is a valid date or not.

    Syntax

    checkdate(month, day, year);
    
    

    Example

    var_dump(checkdate(11,27,2021));
    var_dump(checkdate(2,29,2021));
    
    

    Output:

    bool(true)
    bool(false)
    
  4.  date_diff() function: PHP date_diff() function is used to return the difference between two dates.

    Syntax

    date_diff(datetime1, datetime2, absolute(optional));
    
    

    Example

    $date1=date_create("2021-01-26");
    $date2=date_create("2021-09-10");
    $diff=date_diff($date1,$date2);
    echo $diff->format("%R%a days");
    
    

    Output:

    +227 days
    
  5.  mktime() function: PHP mktime() function is used to return the unix timestamp for a date.

    Syntax

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

    Example

    echo date("M-d-Y h:m:s", mktime(10,11,35,11,27,2021));
    
    

    Output:

    Nov-27-2021 10:11:35