The Wayback Machine - https://web.archive.org/web/20240620215224/https://www.geeksforgeeks.org/how-to-get-the-current-date-and-time-in-php/
Open In App

How to get the current Date and Time in PHP ?

Last Updated : 14 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

  1. d: It represents the day of the month which has two digits with leading zeros (01 or 31)
  2. m: It represents month in numbers with leading zeros (01 or 1)
  3. y: It represents a year in two digits (08 or 14).
  4. h: It represents the hour of the day in two digits with leading zeros (01 or 1)
  5. I: It represents the minute in the current time zone.
  6. s: It represents the number of seconds in the current timezone.

Example 1: 

PHP




<?php 
// PHP program to get the 
// current date and time 
  
echo "Current date and time is :"; 
  
// Store the date and time  to 
// the variable 
$myDate = date("d-m-y h:i:s");
  
// Display the date and time 
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:



Previous Article
Next Article

Similar Reads

How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come after some given date(Ex. 'end date' must be after 'start date')
5 min read
How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come before some given date (Ex: 'start date' must come before 'end da
5 min read
How to get the current date and time in seconds ?
In this article, we will learn how to get the current date &amp; time in seconds using JavaScript build-in methods. We will perform it in 2 ways: Using Date.now() methodUsing new Date.getTime() methodMethod 1: Using Date.now() MethodThe Date.now() method returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. This is known a
2 min read
How to get current date and time in firebase using ReactJS ?
This article provides a detailed walkthrough on obtaining the current date and time in a Firebase environment using ReactJS. Readers will gain insights into the implementation process, enabling date and time functionalities into their React projects powered by Firebase. Prerequisites:Node JS or NPMReact JSFirebaseSteps to Create React Application A
2 min read
How to convert UTC date time into local date time using JavaScript ?
Given an UTC date and the task is to convert UTC date time into local date-time using JavaScript toLocaleString() function. Syntax: var theDate = new Date(Date.parse('DATE_IN_UTC')) theDate.toLocaleString() Example 1: This example converts UTC date time into local date time using JavaScript. C/C++ Code &lt;body&gt; &lt;h1 style=&quot;color:green;
1 min read
How to get the time of the last modification of the current page in PHP?
Sometimes, we need to get the last modification time of the current page in PHP for different uses. Since we are using PHP, we can easily get the last modification time of the current page by using getlastmod() PHP function. Using getlastmod() Function: The getlastmod() function is used to get the last modification time of the current page. This fu
2 min read
How to Get Current Time in Milliseconds in PHP ?
In this article, we will see how to get the current time in milliseconds in PHP. There are mainly three methods to get the current time in milliseconds and they are the following: Table of Content Using microtime() functionUsing DateTime class with the format methodUsing date_create() function with the format methodApproach 1: Using microtime() Fun
1 min read
How to get the first and last date of current month using JavaScript ?
Given a month and the task is to determine the first and last day of that month in proper format using JavaScript. JavaScript getDate() Method: This method returns the day of the month (from 1 to 31) for the defined date. Syntax: Date.getDate() Parameters: This method does not accept any parameters. Return value: It returns a number from 1 to 31, r
3 min read
How to get current formatted date dd/mm/yyyy in Javascript and append it to an input?
The current date in "dd/mm/yyyy" format represents the day, month, and year according to the Gregorian calendar,To obtain the current date in "dd/mm/yyyy" format using JavaScript, utilize the `Date` object methods to extract day, month, and year components, format them accordingly, and then append the formatted date to an input field. Here, we have
3 min read
How to get current formatted date dd/mm/yyyy in JavaScript ?
In this article, we will see how to get the format current date in dd/mm/yyyy format by using JavaScript. We're going to discuss a few methods. The first few methods to know. These are the methods that can be used in solving this problem: Table of Content JavaScript getDate() MethodJavaScript getFullYear() MethodJavaScript getMonth() MethodJavaScri
4 min read