PHP Checking if the current date is before or after a set date

前端 未结 10 637
无人共我
无人共我 2021-02-01 13:10

Im pulling a date from a database which is formatted like dd-mm-YYYY.

What I want to do is check the current date;

  • if the current date is before the date com
相关标签:
10条回答
  • 2021-02-01 13:47

    If you are looking for a generic way of doing this via MySQL, you could simply use a SELECT statement, and add the WHERE clause to it.

    This will grab all fields for all rows, where the date stored in field "date" is before "now".

    SELECT * FROM table WHERE UNIX_TIMESTAMP(`date`) < UNIX_TIMESTAMP()
    

    Personally, I found this method to be more gentle on newbies in MySQL, since it uses the standard SELECT statement with WHERE to fetch the results. Obviously, you could grab only the fields relevant if you wanted to, by listing them instead of using a wildcard (*).

    0 讨论(0)
  • 2021-02-01 13:49
    if(strtotime($row['database_date']) > strtotime('now')) echo $row['database_date'];
    else echo date("d-m-Y");
    

    No need to check the hours because if they are on the same day it will show the same date either way...

    0 讨论(0)
  • 2021-02-01 14:02

    if (strtotime($date) > mktime(0,0,0)) should do the job.

    0 讨论(0)
  • 2021-02-01 14:03

    a MySQL-only solution would be something like this:

    SELECT IF (UNIX_TIMESTAMP(`field`) > UNIX_TIMESTAMP(), `field`,'GO AHEAD') as `yourdate`
    FROM `table`
    
    0 讨论(0)
提交回复
热议问题