How to get the Current Date, Time, and Day in PHP?

Last updated on September 27, 2023

In PHP we can print the current date, time, and day with the help of the date() function. It prints the data related to the date based on the provided arguments.

The date() function prints the date in PHP. It returns the date and time based on passed arguments in the function. The date() function returns the remote server’s date and time.

Note: For printing the date there is a specific string format.

PHP Date Format Strings

1 – Y: It returns a year with four characters.

echo date("Y");
// output: 2023

echo date("d,F,Y D");
// output: 07,September,2023 Thu

In this example, we pass the capital letter Y, which represents full year (current year). So, the above date() is returning the date along with the full year because it has the capital letter Y.

2 – y: It returns a year with two characters.

echo date("y");
// output: 23

In this example we are passing small letter y into the date() function that returns last two digits of current year.

PHP String Format For Month :

1 – F: It returns the month’s full name.

echo date("d,F,y D");
// output: 07,September,23 Thu

echo date("F");
// output: September

In this example, we passed the capital letter F into the date() function which represents the full month’s name.

2 – M: It returns the short name of the month.

echo date("d,M,Y D");
// output: 07,09,2023 Thu
echo date("M");
// output: Sep

In this example, we passed the capital letter M which returns a short textual month’s name.

3 – m: It returns the month in a numeric value along with zero.

echo date("d,m,Y D");
// output: 07,09,2023 Thu
echo date("m");
// output: 09

4 – n: It returns the month in a numeric value without zero.

echo date("d,n,Y D");
// output: 07,9,2023 Thu

PHP String Format For Date :

1 – d: It represents the day of the month with leading zeros (01 to 31).

echo date("d,F,y D");

// Output: 07,September,23 Thu

2 – j: This format is printing the date without zero before 10.

echo date("j,F,Y D");

// Output: 7,September,2023 Thu

3 – S: This format returns us the meaningful date in English.

echo date("dS,F,Y D");

// Output :  07th,September,2023 Thu

PHP String Format For Day :

1 – D: This format is printing the day in a short name.

echo date("d,F,Y D");

// Output: 07,September,2023 Thu

2 – I: This format is printing the day in (full) name.

echo date("d,F,Y l");

// Output: 07,September,2023 Thursday

3 – N: This format prints the day in numeric value and starts the day number from Monday.

echo date("d,F,Y N");

//  Output: 07,September,2023 4

iv- W: This format prints the day in numeric value and starts the day number from Sunday.

echo date("d,F,Y W");

// Output: 07,September,2023 36

Time Format:

PHP String Format For Hours:

1 – h: This format is printing the hours from 01 to 12 with zero.

echo date("h:i:s:a");

// Output: 08:20:17:am

2 – H: This format is printing the hours from 1 to 00 with zero(24 hours).

echo date("H:i:s a");

// Output: 09:48:11 am

3 – g: This format is printing the hours from 1 to 12 without zero.

echo date("g:i:s a");

// Output: 9:56:40 am

4 – G: This format is printing the hours from 0 to 23 (24hr) without zero.

echo date("G:i:s a");

// Output: 10:00:19 am

PHP String Format For Minutes:

1 – i: This format is printing the minutes, for minutes is just one format.

echo date("G:i:s a");

// Output: 10:05:17 am

PHP String Format For Seconds:

1 – s: This format is printing the seconds, for seconds is just one format.

echo date("G:i:s a");

// Output: 10:09:45 am


PHP String Format For Meridiem:

1 – a: This format prints the meridian (am or pm), in small letters.

echo date("G:i:s:a");

// Output: 10:13:10:am

1 – A: This format prints the meridian (AM or PM), in capital letters.

echo date("G:i:s:A");

// Output: 10:16:25:AM


Written by
I am Siraj, a results-driven web developer with three years of hands-on experience in both frontend and backend development. My passion for creating seamless digital experiences drives me to continuously innovate and excel in my field.

Share on:

Related Posts

Improved by: Ayaz Ali Shah

Tags: PHP,