PHP Date and Time

Whenever conducting SQL queries or constructing a website PHP date and time are two of the most commonly used functions in PHP. For these purposes, PHP provides predefined functions. The following sections go over some of PHP’s built-in date and time capabilities.

PHP date() Function

A timestamp is converted to a more readable date and time format using the PHP date() function. We will also see a separate function for a time.

Syntax:

date(format, timestamp)

The date function contains two parameters:

  1. Format: It sets the format of the returned date and time.
  2. Timestamp: The timestamp parameter is optional; if it is not specified, the current date and time will be used.

The following program demonstrates how to use the date() function in PHP:

<?php
  
echo "Today's Date:";
$date = date("d/m/y");
echo $date;
  
?>

Date Function Formatting Options:

The date() function’s format parameter is a string that can contain multiple characters and can be used to generate dates in a variety of formats. The date() function’s format parameter is a string that can contain multiple characters and can be used to generate dates in a variety of formats.

Formatting characters for dates that are often used in format strings include:

dIt represents days of months in numerical form with leading zeros (01 to 31).
DIt represents the day of the week as an abbreviation (Mon to Sun).
mIt represents the month in number two digits with leading zeros (01 to 12).
MIt represents a month in textual form as an abbreviation (Jan to Dec).
yIt represents the year in two digits(08 or 14).
YIt represents the year in four digits(2008 or 2014).
lIt represents the day of the week in textual form.

Other characters, such as hyphens (-), dots (.), slashes (/), or spaces, can be used to divide the components of the date to add further visual styling.

The following examples show how to format today’s date in various ways:

<?php
echo "Today is " . date("d/m/y") . "<br>";
echo "Today is " . date("D/M/Y") . "<br>";
echo "Today is " . date("d-m-Y/D") . "<br>";
echo "Today is " . date("l");
?>

To format the time string using the date() function, the following characters are used:

hIt represents an hour in 12-hour format with leading zeros (01 to 12).
HIt represents an hour in 24-hour format with leading zeros (00 to 23).
iIt represents minutes with leading zeros (00 to 59).
sIt represents seconds with leading zeros (00 to 59).
aIt represents lower case (am and pm).
AIt represents the upper case (am and pm).

The following examples show how to format time in various ways:

<?php
echo "Current Time: " . date("h:i:sa") . "<br>";
echo "Current Time: " . date("h:i:s") . "<br>";
echo "Current Time: " . date("M,d,Y H:i:s A")."<br>";
echo "Current Time: " . date("h:i a");
?>

Create a Date in String using strtotime()

To convert a human-readable date string into a Unix timestamp, the PHP strtotime() function is used:

Syntax:

strtotime(time, now)

The below example set the format of a date and time written in  strtotime() function:

<?php
$d=strtotime("4:20pm June 10 2019");
echo "Created date is " . date("d-M-Y h:i:s A", $d);
?>

String to Date Conversion

Because PHP is so good at translating a text to a date, you can use a variety of values:

<?php
$d=strtotime("yesterday");
echo date("d-m-Y", $d) . "<br>";

$d=strtotime("tomorrow");
echo date("d-m-Y", $d) . "<br>";

$d=strtotime("next Saturday");
echo date("d-m-Y/D", $d) . "<br>";

$d=strtotime("+3 Months");
echo date("d-m-Y/D", $d) . "<br>";
?>