Comparison of php time and date and addition and subtraction examples

Source: Internet
Author: User
Tags current time string format time and date

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.