You currently have JavaScript disabled on your browser.

This website uses JavaScript, and This page needs JavaScript activated to work correctly.

Please active JavaScript on your browser and then refresh this page.

Date and Time



Today is : Tuesday Nov 29, 2022 12:02:06 pm

In this blog post I am referring to the Date and Time feature that is available with server side PHP.

It is important to keep in mind that since the date() function is server side PHP then the returned date and time value is that of the server, and is not that of the client. To get the clients current date and time values you would need to use JavaScript instead of PHP.

The PHP date() function formats a timestamp date into a more manageable date and/or time.

ParameterDescription
formatRequired, Specifies the format of the timestamp
timestampOptional. Specifies a timestamp. If not specified then default value used is the current date and time

Get a Date

The required format parameter of the date() function specifies how to format the date (or time).

Some commonly used date format characters include:

Other characters, such as "/", ".", or "-" can also be inserted between the characters to automatically add additional formatting to the returned value, so that the returned value looks something like "07/15/2021".

Example Source Code

This example demonstrates how to display and format today's date in a variety of different ways:

 
<?php
echo("The day (d) is " . date("d") . "<br>");
echo("The day (D) is " . date("D") . "<br>");
echo("The month (m) is " . date("m") . "<br>");
echo("The month (M) is " . date("M") . "<br>");
echo("The year (y) is " . date("y") . "<br>");
echo("The year (Y) is " . date("Y") . "<br>");
echo("Today (l) is " . date("l") . "<br>");
echo("Today is " . date("Y/m/d") . "<br>");
echo("Today is " . date("Y.m.d") . "<br>");
echo("Today is " . date("Y-m-d") . "<br>");
echo("Today is " . date("l M d, Y") . "<br>");
?>

When you run the above example then you should see something similar to the following:

The day (d) is 29
The day (D) is Tue
The month (m) is 11
The month (M) is Nov
The year (y) is 22
The year (Y) is 2022
Today (l) is Tuesday
Today is 2022/11/29
Today is 2022.11.29
Today is 2022-11-29
Today is Tuesday Nov 29, 2022


Get a Time

Like the date values, the time values can also be extracted and formated for desired use. To get the time values, you still use the PHP date() function, but you pass it different format characters that are related to time.

Some commonly used time format characters are:

Other characters, such as "/", ".", or "-" can also be inserted between the characters to automatically add additional formatting to the returned value, so that the returned value looks something similar to "8:58:13 am".

Example Source Code

This example demonstrates how to display and format the value of time in a variety of different ways:

 
<?php
echo("The hour (H) is " . date("H") . "<br>");
echo("The hour (h) is " . date("h") . "<br>");
echo("The minute (i) is " . date("i") . "<br>");
echo("The second (s) is " . date("s") . "<br>");
echo("The meridiem (a) is " . date("a") . "<br>");
echo("The meridiem (A) is " . date("A") . "<br>");
echo("The current time is " . date("h:i:s a") . "<br>");
?>

When you run the above example then you should see something similar to the following (refresh the page to see these values update):

The hour (H) is 12
The hour (h) is 12
The minute (i) is 02
The second (s) is 06
The meridiem (a) is pm
The meridiem (A) is PM
The current time is 12:02:06 pm


Final Thoughts

Just keep in mind that the returned date and time values are that of the web server and is not necessarily that of the client.

Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.


grayStargrayStargrayStargrayStargrayStar  Be the first to rate this post

User Comments






TJDivisions© 2022