Functional requirements
During the publishing period of an article, you can publish it during the day and stop it at night. Let's assume that the daytime time period is from six o'clock P.M. am (it is estimated that many people are not awake) to AM, so we can
The code is as follows: |
Copy code |
/** * PHP time comparison * Qiongtai blog */ // Define the start time. Note that the time is in string format, so it must be enclosed by quotation marks. Otherwise, do not contact me for an error. In addition, the colon must be an English colon. $ Start_time = '7: 00 '; // Define the end time. Never ask me why I wrote it at six o'clock P.M.. I suggest you reread the primary school. $ End_time = '18: 00 '; // Get the current time period. I will not talk about the use of the date () function. If you don't understand it, read the previous article or google $ Now_time = date ('H: I '); // Judge If ($ start_time <= $ now_time & $ end_time --> = $ now_time ){ Echo 'I want to publish the information! '; } Else { Echo 'Big Brother, what time is it now ~~~ People are not awake yet !!! '; } |
Supplement: date addition and subtraction method
Example: 10:10:00
I want to add 5 months based on this date and return the processed date.
Result: 10:10:00 + 5 months equals 10:10:00
This is also the general meaning of combining the date () and strtotime () functions of PHP functions. Please refer to the instance code.
The code is as follows: |
Copy code |
<? Php /** * Date addition and subtraction method in PHP * Qiongtai old house */ // Step 1, assume there is a time $ A = '2017-04-25 10:10:00 '; // Step 2, obtain the timestamp of this date $ A_time = strtotime ($ ); // Step 3: obtain the timestamp after adding five months $ B _time = strtotime ('+ 5 month', $ a_time ); // Part 4: convert the timestamp back to the date format $ B = date ('Y-m-d H: I: S', $ B _time ); Echo 'This is the date after adding five months '. $ B; // If you think the above code is too long, you can do it in one line. $ B = date ('Y-m-d H: I: S', strtotime ('+'. $ time. 'Month', strtotime ($ ))); Echo 'This is the date after adding five months '. $ B; ?> |
Although the above example does not have any relationship with the first example in the article, I think the comparison of dates and the addition and subtraction of dates can be related.