简体   繁体   中英

Does date fall within 7 days? PHP

Is there a way to find out if a date falls within 7 days of the current date using PHP? If there is, would it also be possible to figure out how many days away that date is?

$date = strtotime('2010-11-28');

if (strtotime('-7 days') < $date && $date < strtotime('+7 days')) {
    // yup
}

$difference = abs($date - time()) / 60 / 60 / 24;

Could be refined a bit if you care about edge cases, whole days and daylight-savings/leap seconds issues, but this should hopefully give you the right idea. Of course the Date class should be the preferred method to handle this, but it's only available in PHP 5.3+.

For the first one, add 7 days to the current date and see which is greater.

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add

For the second one:

http://php.net/manual/en/function.date-diff.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM