简体   繁体   English

PHP和MySQL - 比较成熟日期

[英]PHP and MySQL - Comparing dates near maturity

I'm trying to do a system that compare an specific date ( $date ) with the atual date ( date('Y/m/d') ), but I need to color this $date when he is one week to the atual date (near the maturity date). 我正在尝试建立一个系统,将特定日期( $date )与日期( date('Y/m/d') )进行比较,但是当他在一周时,我需要为这个$date着色日期(到期日附近)。 I don't know if you can understand me, my english is bad... 我不知道你是否能理解我,我的英语很糟糕......

Thank you for reading this. 谢谢您阅读此篇。

Do it in the MySQL query: 在MySQL查询中执行此操作:

SELECT maturity_date,
       maturity_date < DATE_ADD(NOW(), INTERVAL 1 WEEK) AS near_maturity,
       ...

Then your PHP code can use if ($row['near_maturity') to color the date. 然后你的PHP代码可以使用if ($row['near_maturity')来为日期着色。

Something like this may work: 这样的事情可能有用:

date_default_timezone_set('America/Los_Angeles');
$date = DateTime::createFromFormat('m-d-Y', '04-15-2013');
$maturityDate = DateTime::createFromFormat('m-d-Y', '04-20-2013');
$maturityDateMinus10Days = DateTime::createFromFormat('m-d-Y', '04-10-2013');

if ($date > $maturityDateMinus10Days
    && $date < $maturityDate) {
    echo 'date is within 10 days of maturity 10';
}

You can test it by copying and pasting here http://writecodeonline.com/php/ 您可以通过复制和粘贴来测试它http://writecodeonline.com/php/

Ref. 参考。 PHP - add 1 day to date format mm-dd-yyyy PHP - 添加1天的日期格式mm-dd-yyyy

Ref. 参考。 How can I check if the current date/time is past a set date/time? 如何检查当前日期/时间是否超过设定的日期/时间?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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