Arya
June 10, 2021
Date and time are frequently used operation in any server-side programming languages or designing website etc.
The PHP function date() is used to format date/time. It converts timestamp to a more readable date and time format.
Syntax
date(format,timestamp)
- format => format is a required parameter. It specifies how to format the date or time.
- timestamp => timestamp is optional parameter. It specifies a timestamp.
PHP code to get current date and time
<?php
echo date('Y-m-d'); //Display current date
echo "<br>";
echo date('Y-m-d H:i:s'); //Display current date and time
echo "<br>";
echo date("h:i:sa"); //Display the current time
?>
Formating options in Date Functions
- D - Represents the day of the week. (Mon to Sun)
- d - Represents day in a month (1 to 31)
- M - Represents month in the text (Jan to Dec)
- m - Represents month in numbers with leading zeros (01 to 12)
- Y - Represents year in full digits (2020 or 2021)
- y - Represents year in two digits (08 or 14)
- The parts of the date can be separated by the characters like dots (.), slashes (/), hyphens (-), or spaces (additional visual formatting)
0
0