How to get the current date and time in PHP
  John Mwaniki /   11 Sep 2021

How to get the current date and time in PHP

As a website developer, there will be many instances where you want to get the current date and time. This could be when you want to display it on a web page or when you are saving data in the database and would want to keep a record of the time it happens.

Another common scenario is when you want to grant or deny access to a resource based on the current time, where you have to get the current date and compare it with another date.

Getting the current date and time is very easy in PHP.

In this article, you will cover everything you need to know about getting the current date and time in PHP and converting it to many different formats.

The PHP date() function

We use the in-built PHP date() function to get and covert time to different formats.

It returns a string formatted according to the given format string using the given integer timestamp.

It takes the format below:


date($format, $timestamp)

Where $format is a string value and $timestamp is an optional integer Unix timestamp value.

Unix timestamp is the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC. We get the current Unix timestamp using the time() function as shown below:


$timestamp = time();

If $timestamp parameter is not given in the date() function, the function defaults to the value of time(). Therefore, date($format, time()) is equivalent to date($format).


$date = date($format);

Now let's look at the $format string. There are countless ways in which a date and time can be expressed for example below:

  • 2021-09-11
  • 11/09/2021 11:00 AM
  • Sat 11 Sep, 2021
  • 11th September 2021
  • Etc

This $format string provides multiple options on formatting the date and time to your preferred one.

PHP provides you with a long list of characters that you can use to make up your string as shown in the table below:

String Value Description
Y This is a 2-digit year eg. 2021
y This is 2-digit year eg. 21
F The full month name eg. September
M This is 3-letter name of the month eg. Sep
m The number of the month eg. 09 for September
n Numeric representation of a month, without leading zeros (1 to 12)
l (Lower case L) Long-form day of the week eg. Monday
D This is 3-letter day of the week eg. Mon
d Day of the month (01 - 31)
j Day of the month without leading zeros (1 - 31)
S The English ordinal suffix for the day of the month eg. st, nd, rd, th. Works best when used in combination with j.
H The hour in 24hr system (00 - 23)
h The hour in 12hr system (01 - 12)
G 24-hour format of an hour without leading zeros (0 to 23)
g 12-hour format of an hour without leading zeros (1 to 12)
i Minutes with leading zeros (00 to 59)
s Seconds with leading zeros (00 to 59)
A Sets uppercase AM or PM
a Sets lowercase am or pm

Now pass your preferred string values above to the date()function enclosed with quotes.

Examples for expressing current time in PHP

Example 1

Displaying the date as 2021-09-11

<?php
echo date("Y-m-d");
?>

 

Example 2

Displaying the date as 11th September, 2021

<?php
echo date("jS F, Y");
?>

 

Example 3

Displaying the date as Saturday 11th September, 2021

<?php
echo date("l jS M, Y");
?>

 

Example 4

Displaying the date as 11-09-2021

<?php
echo date("d-m-Y");
?>

 

Example 5

Displaying the date as Saturday 11th Sep, 2021 11:00am

<?php
echo date("l jS M, Y  g:i a");

 

Conclusion

Working with dates is an integral part of programming in every language and in the building of almost all systems/applications.

PHP language provides an easy way to work with dates. In this article, you have learned how to get the current date in PHP in different formats as per your preference.