Home > Article > Backend Development > How to get the current date in php
1. Prerequisites
In PHP website programming, we generally use the date function to obtain the time on the server. But in order to call the date function correctly, you must ensure that the php configuration file php.ini has been configured correctly, otherwise an error will be reported when calling the date function and the time cannot be obtained correctly.
2. Use the Notepad program to open the php configuration file php.ini and look for the "date.timezone = " item. If there is ";" in front of it, remove the previous one. ";", and change the value of this item to: date.timezone = PRC or date.timezone = "Asia/Shanghai". After saving the configuration, you need to restart the server for the modified configuration to take effect.
3. Get the date
After the configuration file is modified, we can call the date function to get the current server time. The relevant code is as follows:
<?php echo "当前系统日期为:".date('Y-m-d'); >
Check the running effect in the browser and you can see that the server time is displayed successfully!
#4. Format and adjust the display format of the obtained date.
The following code can display a date format that is more in line with our Chinese habits:
<?php echo "当前系统日期为:".date('Y').'年'.date('m').'月'.date('d').'日'; >
Recommended tutorial: PHP video tutorial
The above is the detailed content of How to get the current date in php. For more information, please follow other related articles on the PHP Chinese website!