Home  >  Article  >  Backend Development  >  PHP functions date and time functions

PHP functions date and time functions

WBOY
WBOYOriginal
2023-05-19 08:00:081479browse

PHP is a widely used high-level open source programming language that can handle various tasks with ease. One very important aspect is date and time. In PHP, there are many built-in functions for working with dates and times. In this article, we will introduce the date and time functions of PHP functions.

  1. Get the current date and time

There is a function in PHP called date(), which allows developers to get the current date and time. This function accepts two parameters: format and timestamp. The first parameter specifies the format of the date and time, and the second parameter specifies the timestamp to use. If no timestamp is specified, the function will use the current time as the default value.

The following are some common date and time formats:

  • Y: Year, 4 digits
  • y: Year, 2 digits
  • m : Month, 2 digits
  • M: Month, 3 digits English abbreviation
  • d: Date, 2 digits
  • D: Day of the week, 3 digits English abbreviation
  • H: Hour, 24-hour format, 2 digits
  • h: Hour, 12-hour format, 2 digits
  • i: Minutes, 2 digits
  • s: Seconds, 2 digits

For example, to get the current date and time, you can use the following code:

$date_time = date('Y-m-d H:i:s');
echo $date_time;

The above code will output a result similar to the following:

2019-01-01 12:00:00
  1. Get the timestamp of the specified date and time

In addition to getting the timestamp of the current date and time, you can also use the PHP built-in function strtotime() to get the timestamp of a specific date and time. This function accepts one parameter: a string representing the date and time. The string can be in any format for date and time. The strtotime() function will parse the string and return the Unix timestamp for that date-time.

For example, to get the timestamp of January 1, 2019, you can use the following code:

$timestamp = strtotime('2019-01-01');
echo $timestamp;

The above code will output results similar to the following:

1546300800
  1. Convert timestamp to date and time

PHP built-in function date() can not only get the current date and time, but also convert Unix timestamp into a readable format. Simply pass the timestamp as the second parameter of the date() function to convert the timestamp into datetime format.

For example, to convert a timestamp to a datetime, you can use the following code:

$timestamp = 1546300800;
$date_time = date('Y-m-d H:i:s', $timestamp);
echo $date_time;

The above code will output results similar to the following:

2019-01-01 00:00:00
  1. Get Day of the week for a specified date and time

The date() function in PHP can also be used to obtain the day of the week for a specified date and time. Simply pass "D" as the first argument to the date() function to get the 3-letter abbreviation of the day of the week.

For example, to get whether January 1, 2019 is a Tuesday, you can use the following code:

$timestamp = strtotime('2019-01-01');
$day_of_week = date('D', $timestamp);
echo $day_of_week;

The above code will output results similar to the following:

Tue
  1. Get the time difference between two dates

The timestamp in PHP can compare different dates to calculate the time difference between two dates. This can be achieved by converting two dates to timestamps and then subtracting them.

For example, to calculate the time difference between January 1, 2019 and February 1, 2019, you can use the following code:

$start_date = strtotime('2019-01-01');
$end_date = strtotime('2019-02-01');
$time_diff = round(($end_date - $start_date) / (60 * 60 * 24));
echo $time_diff . ' days';

The above code will output a result similar to the following :

31 days

Summary

Such a powerful date and time function library, for PHP developers, can not only improve development efficiency, but also quickly process large amounts of data. It will be very helpful for developers to be proficient in the above functions and understand some common date and time formats.

The above is the detailed content of PHP functions date and time functions. 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