Home  >  Article  >  Backend Development  >  Compare two dates in php_PHP Tutorial

Compare two dates in php_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:51:32968browse




Compare two dates

The YMD has been divided into numbers in the database. For example, Y=2020 M=1 D=1
After I take it out now, it displays as daydiff=date( "$nowyr"."$nowmon"."$nowdt") - date("Ymd");
But after this subtraction, the digital operation is negative, 202011-20090305
How can I convert it to 20200101-20090305
That is, the time retrieved from the database is compared with the current time, and the date is subtracted. If the date is after the current time, it is positive, otherwise it is negative.
Please help me heroes.


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 = "2010-01-01 00:00:00";
if((strtotime($onetime)-time()) {
//
}
elseif((strtotime($onetime)-time())>0)
{
//
}
else
{
//
}



D8888D reply content------------------------------------------------- ----------
$onetime = "2010-01-01 00:00:00";
if((strtotime($onetime)-time()) {
//
}
elseif((strtotime($onetime)-time())>0)
{
//
}
else
{
//
}

D8888D reply content------------------------------------------------- ----------
If I don't do that, I can consider converting it to a timestamp [img]http://www.111cn.cn/bbs/images/smilies/default/victory.gif[/img]

D8888D reply content------------------------------------------------- ----------
I tried, but got an error,
echo date ("M-d-Y", mktime (0,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");


Copy code

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

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

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

D8888D reply content------------------------------------------------- ----------
When saving the date, save a timestamp by the way. Subtracting the date from the date is meaningless. The rounding up of the year, month, and day is inconsistent, and the subtracted value is meaningless

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

I wrote it like this. There seems to be no problem with testing fixed numbers, but the year, month, day, and number of digits are sometimes different. I don’t know if there are times when it doesn’t hold true. Why! Not sure yet. . . . . . . .


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632544.htmlTechArticleWhen comparing two dates, YMD has been divided into numbers in the database. For example, Y=2020 M=1 D=1 After I take it out now, it is displayed as daydiff=date( "$nowyr"."$nowmon"."$nowdt") - date("Ymd");...
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