Introduction
"PHP: Hypertext Preprocessor" acronyms are known as PHP. It is an open-source scripting language whose scripts run on the server and return to the browser as plain HTML. PHP is a language whose file can contain HTML, CSS, Javascript, or PHP code. These files have extension ".php". This language has many applications such as collecting form data, creating, opening, reading, deleting, writing, closing files on the server, etc.
This blog focuses on PHP date and time functions used to format date and time. It has various formatting options, predefined functions, and other operations. We use the date() function for formatting date or time. Timestamp gets formatted with the date() function to a more readable date and time. Our computer stores date and time in UNIX timestamp, for example, midnight India Mean Time on January 1, 1999, i.e., January 25, 1999, 23:05:40 GMT.
How to get the date in PHP?
First, we will see the familiar characters we use for dates:
- The day of the month, which is from 1 to 31, is represented by ‘d’ character
- A month which 1 to 12 is defined by ‘m’ character
- A year that appears in four digits is represented by the ‘y’ character.
- We represent the day of the week with ‘l’ ( lowercase 'L')
For adding additional formating to the date, we use characters like "/", ".", or "-" in-between characters.
Now we will see how to get a date.
date(format,timestamp)
Here the parameter format is required, which specifies the format of the timestamp.
The parameter timestamp is optional, which specifies timestamp. By default it specifies the current date and time.
First of all, we will pass the required parameter of the date function with is the format.
<?php
echo "<h2>Coding ninja</h2></br>";
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>
Output-