Home  >  Article  >  Backend Development  >  How to determine whether it is the time of day in php (two methods)

How to determine whether it is the time of day in php (two methods)

PHPz
PHPzOriginal
2023-04-12 09:21:342057browse

In the PHP language, it is a very common operation to determine whether a time is the time of the day, because many applications need to display different information based on the current time. There are several ways to achieve this functionality.

Method 1: Use the date function to obtain the current date and the date to be judged

PHP's date function can obtain the current date and time and format the output in the specified format.

For example, the following code will output the current date and time:

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

The 'Y-m-d H:i:s' here means that the output date format is "year-month-day hour: minute: Second".

In order to determine whether a date is today's date, we can store the current date in the same format, and then compare whether the two dates are the same. The specific implementation code is as follows:

$today = date('Y-m-d');
$target_date = '2021-04-23';

if ($today == $target_date) {
   echo '是当天日期';
} else {
   echo '不是当天日期';
}

Here, first use the date function to obtain the current date and store it in the $today variable, and then store the date that needs to be judged in the $target_date variable. Next, use an if statement to compare the two dates. If they are the same, output "is today's date", otherwise output "is not today's date".

Method 2: Use the strtotime function to convert the date to a timestamp for comparison

PHP's strtotime function can convert a date to a timestamp, and the timestamp is an integer, which represents since 1970 The number of seconds elapsed since January 1, year.

The specific implementation code is as follows:

$target_date = '2021-04-23';
$target_timestamp = strtotime($target_date);
$current_timestamp = time();

if ($target_timestamp >= strtotime('today') && $target_timestamp < strtotime('tomorrow')) {
   echo '是当天日期';
} else {
   echo '不是当天日期';
}

Here, first store the date that needs to be judged in the $target_date variable, then use the strtotime function to convert it into a timestamp, and store it in the $target_timestamp variable . Next, use the time function to get the current timestamp and store it in the $current_timestamp variable.

Then, use the if statement to determine whether $target_timestamp is greater than or equal to the starting timestamp of the day (that is, the timestamp at zero o'clock today), and less than tomorrow's starting timestamp. If it is, it means that $target_date is today's date, and output "is today's date", otherwise output "is not today's date".

Note: The timestamp of "0 o'clock today" here can be obtained using strtotime('today'), and the "starting timestamp of tomorrow" can be obtained using strtotime('tomorrow').

Summary

The above are two methods of judging whether a date is today's date. The first method has a simpler idea, but if the time range that needs to be compared is a certain time period, Not only for the current day, more complex calculations need to be performed in conjunction with the timestamp. The second method uses PHP's own strtotime function to convert and compare timestamps, which is more convenient and faster. Everyone can choose flexibly according to actual needs.

The above is the detailed content of How to determine whether it is the time of day in php (two methods). 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