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.
Parameter | Description |
---|---|
format | Required, Specifies the format of the timestamp |
timestamp | Optional. Specifies a timestamp. If not specified then default value used is the current date and time |
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".
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 11Like 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".
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 15Just 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.
About | Contact Us | Privacy | Terms & Conditions | © 2024 - T&J Divisions, LLC, All Rights Reserved |