Examples of comparing time sizes in PHP

  • 2021-07-16 01:59:16
  • OfStack

What is the time comparison of PHP in Shenma Department?

The simplest example is to compare two times with a greater than less sign. Sometimes when we design a program, we need to control the user to operate a certain function, which can only be operated within a certain period of time, and the rest of the time period is not allowed.

For example, I have a website that publishes messages. I publish messages on this website, and then I send them to thousands of users through this message group. According to the common practice, it should be that once a message is released, it will be sent to thousands of users immediately. In this way, as long as 1 has the latest news, the user will receive the mobile phone short message in the first time. However, here comes the problem. For example, if I post information in the early hours of the night, it will be met by all users XXOO.

Therefore, time comparison can be used here. If it can be published during the day, it can be blocked at night. Let's assume that the daytime period is from 7 am (it is estimated that many people are not awake at 7 am) to 6 pm, so we can do this.


/**
 * PHP Time comparison
 */
// Define the start time, note that the time is a string format, so it must be caused by quotation marks, otherwise don't call me for errors, and the colon inside must be an English colon
$start_time = '7:00';
 
// Define the end time, don't ask me afternoon 6 Why is the point written 108 I would advise you to reread elementary school
$end_time  = '18:00';
 
// Gets the current time period, date() Function of the use of my nonsense, do not understand the direct look at previous articles or google
$now_time  = date('H:i');
 
// Judge
if( $start_time<=$now_time && $end_time>=$now_time ){
     echo ' I'm going to release information! ';
}else{
     echo ' Eldest brother, what time is it now ~ ~ ~ people haven't woken up yet! ! ! ';
}

Here need to remind, get the current time is the server time, so please pay attention to the server time zone settings and whether it is on time, or you can come to a planning task to make it proofread everything every day OK


Related articles: