简体   繁体   English

将表格中的日期与今天的日期进行比较

[英]Compare date in a table to todays date

Im trying to figure out how can I make an if statement to compare todays date with a date field in a table. 我试图弄清楚如何制作if语句来将今天的日期与表中的日期字段进行比较。

<?php 
    $dayte_today = date('Y-m-d');
    $plannerid = $_GET["plannerid"];

    $sql="SELECT * FROM planner_content WHERE planner_id='$plannerid'";
    $result=mysqli_query($con,$sql)or die(mysqli_error($con));

    // Mysql_num_row is counting table row
    $count=mysqli_num_rows($result);

    if($count>=1){ 
    /* echo "Your id is $userID"; */
        while ($link =  mysqli_fetch_object($result)) {
        echo ''.$link->date.' - '.$link->comment.'' ;
        }

    }
    else {
        echo "No events yet";
    }
?> 

I've got it to show results for the moment, but I don't know how to compare the actual date with the date in the table. 我现在可以显示结果,但是我不知道如何将实际日期与表中的日期进行比较。 I tried to use mysqli_fetch_object but I didn'y get it to work. 我尝试使用mysqli_fetch_object,但没有使它正常工作。

For comparing dates, you can use the combination of date() and strtotime functions 为了比较日期,可以结合使用date()和strtotime函数

if($count>=1){ 
    /* echo "Your id is $userID"; */
        while ($link =  mysqli_fetch_object($result)) {
        $planned_date = date('Y-m-d', strtotime($link->date));
        /* Do your comparison here */
           $dayte_today - $planned_date;
        }

    }
    else {
        echo "No events yet";
    } 

You can also filter from the mysql query using a query like this 您也可以使用这样的查询从mysql查询中过滤

SELECT * FROM planner_content WHERE planner_id='$plannerid' AND date < curdate();

Why not compare dates using mysql's built-in date functions ? 为什么不使用mysql的内置日期函数比较日期呢?

Add a column to your select statement that uses datediff() to return a number that you can use in your application logic. 在使用datediff()返回要在应用程序逻辑中使用的数字的select语句中添加一列。

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

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