How to get current date and time in PHP
PHP

How to get current date and time in PHP

Editorial Staff
Editorial Staff
08 Dec 2023
2 mins read

In this quick guide, we'll show you the simple steps to get the current date and time and display in PHP using date functions as well as DateTime Class, making your web applications more dynamic and user-friendly.

Before proceeding, it is essential to ensure that your application's timezone has been properly configured. You can refer to the guide on how to get and set timezone in PHP.

How to get the current date

The simplest way to get the current date in your desired format is to use PHP in-built date() function, 

If you would like to view the full list of date and time parameters, you can find them on their official website.

echo date("Y-m-d"); // example 2023-01-30
echo date("m/d/Y"); // example 01/30/2023

Output

2023-01-30 [based on our current date]
01/30/2023 [based on our current date]

Another method for obtaining the current date in an Object-Oriented manner is to utilize the PHP DateTime Class.

Simply instantiate an object and call the format method with your desired parameters.

$date = new DateTime(); 
echo $date->format("Y-m-d");
echo $date->format("m/d/Y");

Output

2023-01-30 [based on our current date]
01/30/2023 [based on our current date]

How to get the current time

Using the PHP date() function, you can get the current time in a similar way as described above. The only thing you need to do is change the parameters.

echo date("H:i:s"); // example 13:25:45
echo date("h:i A"); // example 01:25 PM

Output

13:25:45 [based on our current time]
01:25 PM [based on our current time]

Similarly, for those who prefer an Object-Oriented approach, the PHP DateTime Class can be used. By setting the desired parameters, you can effectively retrieve the current time.

$date = new DateTime(); 
echo $date->format("H:i:s"); // example 13:25:45
echo $date->format("h:i A"); // example 01:25 PM

Output

13:25:45 [based on our current time]
01:25 PM [based on our current time]

Get current date and time together

By now, you have learned how to obtain the date and time separately. Now, let's combine both.

To get the current date and time using the PHP date() function, use the following snippet.

echo date("Y-m-d H:i:s");
echo date("m/d/Y h:i A");

Output

2023-01-30 13:25:45 [based on our current date and time]
01/30/2023 01:25 PM [based on our current date and time]

To get the current date and time using the PHP DateTime Class, use the following snippet.

$date = new DateTime();
echo $date->format("Y-m-d H:i:s");
echo $date->format("m/d/Y h:i A");

Output

2023-01-30 13:25:45 [based on our current date and time] 
01/30/2023 01:25 PM [based on our current date and time]

Happy Coding!

Editorial Staff

Editorial Staff

Editorial Staff at Code Snipps is a team of experts with over 15+ years of experience in Back-end and Front-end Development especially PHP, MySQL, Laravel, JavaScript, etc. We are your go-to destination for quick and reliable code snippets.

You may also like

06 Dec 2023
1 min read