The purpose of the article is to get the current Date and Time in PHP. It is performed by using a simple in-built PHP function date(). The Date is an inbuilt function used to format the timestamp. The computer stores date and time in UNIX timestamp. This time is measured as a number of seconds since Jan 1, 1970. As this is difficult for humans to read, PHP converts timestamps to format in such a way that it is readable and more understandable to humans.
Syntax:
date('d-m-y h:i:s');
Parameters : The date() have different parameters. Each parameter represents some meaningful unit.
- d: It represents the day of the month which has two digits with leading zeros (01 or 31)
- m: It represents month in numbers with leading zeros (01 or 1)
- y: It represents a year in two digits (08 or 14).
- h: It represents the hour of the day in two digits with leading zeros (01 or 1)
- I: It represents the minute in the current time zone.
- s: It represents the number of seconds in the current timezone.
Example 1:
PHP
<?php
echo "Current date and time is :" ;
$myDate = date ( "d-m-y h:i:s" );
echo $myDate ;
?>
|
Output:
Current date and time is :03-01-21 04:49:52
Example 2: The following demonstrates other date() functions format which can be used by the developer as per the need.
PHP
<!DOCTYPE html>
<html>
<body>
<h2>Other ways of using date () function </h2>
<?php
echo "Date in Year.Month.Day format is: "
. date ( "Y.m.d" ) . "<br>" ;
echo "Date in Year-Month-day format is: "
. date ( "Y-m-d" ) . "<br>" ;
echo "Date in Year/Month/day format is: "
. date ( "Y/m/d" ) . "<br>" ;
?>
</body>
</html>
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
14 Jan, 2021
Like Article
Save Article