Home > Backend Development > PHP Tutorial > Best date and time functions in PHP

Best date and time functions in PHP

王林
Release: 2023-05-23 14:14:01
Original
1021 people have browsed it

In PHP, the use of date and time functions is very important. Whether it is processing timestamps, calculating time differences, formatting output times, etc., PHP's date and time functions are very powerful and flexible. However, due to the numerous date and time functions in PHP, some developers may be confused as to which function to choose to complete a specific task. Therefore, this article will introduce some of the most commonly used and best date and time functions in PHP.

  1. time() Function The time() function returns the timestamp of the current time (number of seconds since January 1, 1970). If no argument is given, the current server time will be used. For example:
  2. echo time();
    // 输出当前时间的时间戳
    Copy after login

strtotime() function

  1. strtotime() function can convert any English text datetime description into a timestamp. For example, passing the string "1 day" to the strtotime() function will return tomorrow of the current date. Here are some examples:
  2. echo strtotime("now");
    // 输出当前时间的时间戳
    
    echo strtotime("+1 day");
    // 输出明天此时的时间戳
    
    echo strtotime("+1 week");
    // 输出一周后此时的时间戳
    
    echo strtotime("next Monday");
    // 输出下一个星期一此时的时间戳
    
    echo strtotime("2021-12-25 12:00:00");
    // 输出指定日期时间的时间戳
    Copy after login

date() function

  1. date() function can format the output date and time. It accepts a format string parameter to specify the format of the output results. Here are some commonly used formatting parameters:
  2. echo date("Y-m-d");
    // 输出当前日期,格式为“年-月-日”
    
    echo date("Y-m-d H:i:s");
    // 输出当前日期时间,格式为“年-月-日 时:分:秒”
    Copy after login

mktime() function

  1. mktime() function can be used to create timestamps. It accepts multiple parameters including hour, minute, second, month, day, year, etc. to create a timestamp for a specific date time. The following is an example:
  2. $timestamp = mktime(0, 0, 0, 10, 1, 2021);
    echo $timestamp;
    // 输出 1633046400,即 2021 年 10 月 1 日 00:00:00 的时间戳
    Copy after login

Calculation of time() and mktime()

  1. Using the time() function and mktime() function, you can easily calculate any two The time difference between dates. For example, the following example calculates the time difference between the current date and January 1, 2022:
  2. $today = time();
    $future = mktime(0, 0, 0, 1, 1, 2022);
    $diff = $future - $today;
    
    echo "还有 " . floor($diff / (60 * 60 * 24)) . " 天到 2022 年 1 月 1 日。";
    // 输出“还有 X 天到 2022 年 1 月 1 日。”
    Copy after login

Combined use of strtotime() and date()

  1. strtotime() function and the date() function can be used together to format the output of a string specifying a date and time. For example, the following example will output the current date and time in the "Year-Month-Day Hour:Minute:Second" format:
  2. echo date("Y-m-d H:i:s", strtotime("now"));
    // 输出当前日期时间,格式为“年-月-日 时:分:秒”
    Copy after login

DateTime object

  1. DateTime is a user PHP built-in class for handling dates and times. It provides some convenient methods such as add(), sub(), diff(), etc. to facilitate processing of dates and times. Here is an example using the DateTime class:
  2. $date = new DateTime();
    $date->add(new DateInterval('P1D'));
    echo $date->format('Y-m-d');
    // 输出当前日期的明天,格式为“年-月-日”
    Copy after login
Summary

In PHP, the use of date and time functions is very important. Whether you are processing timestamps, calculating time differences or formatting output times, you need to use date and time functions to complete it. Because each website needs to use different time formats and various customized time functions over time, it is very necessary to be familiar with the use of date and time functions. In this article, we introduce some of the most commonly used and best date and time functions in PHP for developers to learn from.

The above is the detailed content of Best date and time functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template