Date and Time in PHP |
| Written by phpecho.com | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
PHP provides a variety of date and time command variances which makes it very versatile. The entire command structure is very modular which means that you will be able to get only the date, only the day or a combination. In fact, there are a large number of combinations possible using the date and time functions. In this tutorial, we will show how to use these and also a list of all the available functions. The first example, we use shows how to print the current date and time to the console (the browser in our case). <?php print date("Y:m:d"); ?>
This will output the current date separating the year, month and date by “:” symbol. We can also get the same output using the following syntax: <?php print date("Y").":".date("m").":". date("d"); ?>
As you will notice from the above, PHP allows you to use each part of the date separately as well as together. That is the power of the date and time command in PHP. Using the above, you can create a multitude of features for your web applications. For example, suppose you want to welcome your user in a grandiose way. The below example is an option: <?php print (‘Welcome ‘.$userName); print(‘Today is the ‘.date(“d”).”th day of the“.date(“m”).”th month of “.date(“Y”).” year”); ?> The below table gives a list of all available parameters for this function.
|