user2274075
user2274075

Reputation: 117

How to compare a system date with the date from MySQL database in php

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"));

Upvotes: 0

Views: 928

Answers (3)

DevZer0
DevZer0

Reputation: 13535

in your query let say you pass in the value from $now = strttotime("now"); you can then do the following in MySQL

WHERE DATE_FORMAT($now "%Y-%m-%d") = DATE_FORMAT(DATE_ADD(DATE_FIELD,INTERVAL -10 DAY), "%Y-%m-%d")

Upvotes: 1

Patrick Evans
Patrick Evans

Reputation: 42736

Just convert the db table time to a normal unix timestamp subtract and divide by number seconds in a day

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

$daysleft = ($now-$dbtime) / ( 60*60*24);

Upvotes: 0

Dennis Fischer
Dennis Fischer

Reputation: 874

Use date_diff()-> DateTime::diff

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

More functionality can be found here:

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

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

Upvotes: 2

Related Questions