Home  >  Article  >  Backend Development  >  What are the common date and time functions in PHP programming?

What are the common date and time functions in PHP programming?

王林
王林Original
2023-06-12 11:58:331403browse

PHP is a widely used server-side programming language. There are many functions related to date and time in PHP. These functions can be used to obtain and manipulate dates and times, making programming more efficient and flexible. This article will introduce common date and time functions in PHP programming.

  1. date() function

date() function is the basic function for processing dates and times in PHP and is used to format dates and times. This function has two required parameters: the first parameter is a format string, specifying the format of the return value, and the second parameter is an optional timestamp. If not specified, the current time is used by default.

Example:

echo date("Y-m-d");  // 输出当前日期,格式为:年-月-日
echo date("h:i:s a"); // 输出当前时间,格式为:小时:分钟:秒 上午/下午
  1. time() function

time() function returns the timestamp of the current time (since January 1, 1970 00 :00:00 GMT). This function does not require any parameters.

Example:

echo time(); // 输出当前时间戳
  1. strtotime() function

strtotime() function can convert a date string into a timestamp. This function has a required parameter, which is a date string. The format can be any common date format, or it can be an English time description, such as "tomorrow" or "next Monday".

Example:

echo strtotime("2019-10-01"); // 输出该日期对应的时间戳
echo strtotime("next Monday"); // 输出下一个周一对应的时间戳
  1. mktime() function

mktime() function can return a timestamp based on the given time parameter. This function has six optional parameters, representing hours, minutes, seconds, months, days, and years. If not all of these parameters are specified, the current time is used by default.

Example:

echo mktime(0, 0, 0, 10, 1, 2019); // 输出2019年10月1日0点0分0秒对应的时间戳
  1. strftime() function

strftime() function formats the local time/date according to the specified format string. This function has two parameters, the first parameter is a format string, and the second parameter is an optional timestamp, which defaults to the current time.

Example:

setlocale(LC_TIME, 'zh_CN.utf8'); // 设置本地化信息,输出中文日期格式
echo strftime('%Y年%m月%d日', time()); // 输出当前日期,格式为:年月日
  1. gmdate() function

gmdate() function is similar to date() function, used to format date and time, The difference is that the gmdate() function returns Greenwich Mean Time (GMT), while the date() function returns local time. The parameter usage of this function is the same as the date() function.

Example:

echo gmdate("Y-m-d"); // 输出当前GMT日期,格式为:年-月-日

To sum up, the common date and time functions in PHP are: date(), time(), strtotime(), mktime(), strftime() and gmdate() etc. Proficiency in these functions will make PHP programming more efficient and flexible.

The above is the detailed content of What are the common date and time functions in PHP programming?. 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