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.
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:
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
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
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
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
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!