0

I want to compare my system date from the date from MySQL database and the comparison should be like if the system date has the diffrence of 10 days than the date from my table's date.

For e.g. my sytem date: 2013-7-31 and the table's date is 2013-8-10 it should echo a msg "10 days left for renewal."

I know how to add days into a date in php but I have no idea of how should I compare these two.

$mydt = date("Y-m-d");
$alertday = date('Y-m-d', strtotime($mydt. " + 10 days"));
4

3 回答 3

2

使用date_diff()->DateTime::diff

$interval  = date_diff($date1, $date2); //$date1, $date2 are DateTime objects.
echo $interval->d; //Outputs number of day difference.

更多功能可以在这里找到:

http://www.php.net/manual/de/class.dateinterval.php

http://www.php.net/manual/de/datetime.diff.php

于 2013-07-31T10:39:54.953 回答
1

在您的查询中,假设您传递了来自$now = strttotime("now"); 您的值,然后可以在 MySQL 中执行以下操作

WHERE DATE_FORMAT($now "%Y-%m-%d") = DATE_FORMAT(DATE_ADD(DATE_FIELD,INTERVAL -10 DAY), "%Y-%m-%d")
于 2013-07-31T10:44:40.897 回答
0

只需将 db 表时间转换为正常的 unix 时间戳减去并除以一天中的秒数

$now = time();
$dbtime = strtotime($tabledate);

$daysleft = ($now-$dbtime) / ( 60*60*24);
于 2013-07-31T10:40:09.160 回答