Home  >  Article  >  Backend Development  >  How to get the current date in php

How to get the current date in php

王林
王林Original
2019-09-24 13:04:3418429browse

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.

How to get the current date in php

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.

How to get the current date in php

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(&#39;Y-m-d&#39;);
>

Check the running effect in the browser and you can see that the server time is displayed successfully!

How to get the current date in php

#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(&#39;Y&#39;).&#39;年&#39;.date(&#39;m&#39;).&#39;月&#39;.date(&#39;d&#39;).&#39;日&#39;;
>

How to get the current date in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn