Php compares two date_php tutorials-PHP tutorials

Source: Internet
Author: User
Php compares two dates. Comparing two dates, YMD has been divided into numbers in the database. for example, Y2020M1D1 is displayed as daydiffdate ($ nowyr. $ nowmon. $ nowdt)-date (Ymd); compare two dates

YMD has been divided into numbers in the database. for example, Y = 2020 M = 1 D = 1
After I extract it, it is displayed as daydiff = date ("$ nowyr". "$ nowmon". "$ nowdt")-date ("Ymd ");
After this subtraction, the number is calculated as negative, 202011-20090305.
How can I convert to 20200101-20090305?
That is, compare the retrieved time in the database with the current time, and subtract the date. if it is the date after the current time, it is positive, otherwise it is negative.
Please help.


Best answer [url = http://www.111cn.cn/bbs/space.php? Username = doing] link tag doing [/url]
[Url = http://www.111cn.cn/bbs/space.php? Uid = 97304] link tag [img] http://www.111cn.cn/server/avatar.php? Uid = 97304 & size = small [/img] [/url] $ onetime = "00:00:00 ";
If (strtotime ($ onetime)-time () <0)
{
//
}
Elseif (strtotime ($ onetime)-time ()> 0)
{
//
}
Else
{
//
}



D8888D reply content -------------------------------------------------------
$ Onetime = "00:00:00 ";
If (strtotime ($ onetime)-time () <0)
{
//
}
Elseif (strtotime ($ onetime)-time ()> 0)
{
//
}
Else
{
//
}

D8888D reply content -------------------------------------------------------
If this is not the case, you can convert it into a timestamp [img] http://www.111cn.cn/bbs/images/smilies/default/policy.gif#/img]

D8888D reply content -------------------------------------------------------
I tried, but an error occurred,
Echo date ("M-d-Y", mktime (0, 0, $ nowdt, $ nowmon, $ nowyr)-date ("Ymd ");

D8888D reply content -------------------------------------------------------
$ Nowyr = 2020;

$ Nowmon = 1;

$ Nowdt = 1;

Echo date (sprintf ("% 04d % 02d % 02d", $ nowyr, $ nowmon, $ nowdt)-date ("Ymd ");




D8888D reply content -------------------------------------------------------
Timestamp

D8888D reply content -------------------------------------------------------
Mktime (0, 0, 0, $ nowdt, $ nowmon, $ nowyr)-time ()

D8888D reply content -------------------------------------------------------
Timestamp

D8888D reply content -------------------------------------------------------
Save a timestamp when saving the date. it doesn't make sense to subtract the date. the year, month, and day carry numbers are inconsistent, and the value is meaningless.

D8888D reply content -------------------------------------------------------
$ Daydiff = (strtotime (date ("$ nowyr ". "-". "$ nowmon ". "-". "$ nowdt")-strtotime (date ("Y-m-d")/86400;

I wrote it like this. it seems that there is no problem to test fixed numbers, but sometimes the number digits are different in the year, month, and day. I don't know if it is true or not. Ah! Not sure ........


Shards have split YMD into numbers in the database. for example, if Y = 2020 M = 1 D = 1, after obtaining it, it is displayed as daydiff = date ("$ nowyr ". "$ nowmon ". "$ nowdt")-date ("Ymd ");...

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: info-contact@alibabacloud.com 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.