Open phpvariables.php and save it as phpdate.php. At the end of the PHP (after the echo of the number of months) add this code:
$date=date("Y:m:d"); echo "<p>As today is $date</p>";
Save, upload and try. The date function allows you to format dates any way you want. The formatting is done by the letters inside the quotes. Make these changes:
- replace the : characters with slashes or dashes
- change the order of the date components to suit your country's customs (just one country will have nothing to change!)
- change the month number to a text month (M) to avoid confusion in any other countries
- add a space then hours (h), minutes (i) and seconds (s)
There are many other options as shown in the table on this page. For example this is the international standard way of showing dates and times:
and this is the W3C standard way:
Create some more lines of PHP to show those two as well as the first example.
If you used the same variable name ($date) for all it will work and is not a problem. However, it might be best to have different variable names so that all three formats are available for using again. If you assign another value to a variable it replaces the original one.
Times and dates other than now
As well as saying what the date is now you can ask for a formatted date best on any time. The time has to be provided as the number of seconds since 1st January 1970 (known as a Unix timestamp):
Don't worry too much about this as SQL is better at dates than PHP so you may not need to do much with PHP and dates once you learn SQL.