JavaScript Error

You currently have JavaScript disabled on your web browser.

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

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



Date and Time



Today is : Thursday Apr 18, 2024 08:21:58 pm (server timer)

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 18
The day (D) is Thu
The month (m) is 04
The month (M) is Apr
The year (y) is 24
The year (Y) is 2024
Today (l) is Thursday
Today is 2024/04/18
Today is 2024.04.18
Today is 2024-04-18
Today is Thursday Apr 18, 2024


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 20
The hour (h) is 08
The minute (i) is 21
The second (s) is 58
The meridiem (a) is pm
The meridiem (A) is PM
The current time is 08:21:58 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.


(0) pollYesResult
(0) pollNoResult



 
     About   |   Contact Us   |   Privacy   |   Terms & Conditions   |   © 2024 - T&J Divisions, LLC, All Rights Reserved