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:
- Format: It sets the format of the returned date and time.
- 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:
d | It represents days of months in numerical form with leading zeros (01 to 31). |
D | It represents the day of the week as an abbreviation (Mon to Sun). |
m | It represents the month in number two digits with leading zeros (01 to 12). |
M | It represents a month in textual form as an abbreviation (Jan to Dec). |
y | It represents the year in two digits(08 or 14). |
Y | It represents the year in four digits(2008 or 2014). |
l | It 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:
h | It represents an hour in 12-hour format with leading zeros (01 to 12). |
H | It represents an hour in 24-hour format with leading zeros (00 to 23). |
i | It represents minutes with leading zeros (00 to 59). |
s | It represents seconds with leading zeros (00 to 59). |
a | It represents lower case (am and pm). |
A | It 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>"; ?>