简体   繁体   中英

How to check if the previous month has passed in mysql and php?

I have a button 'Save' and a textbox populated with a value of the current date in php.

After the user press the button save, my php script must check if a previous month has passed. For example:

textbox value = '2015-02-01' (let's say its the current date now),

then the user click the 'Save' button.

My php script must say that the date of '2015-01-01' has passed and your in the month of '2015-02-01'. I really it for my if condition as:

//if a month has passed, 
    # echo "Your in the new month";
//else
    # echo "Your still in this month";

I think this has to do with this query:

"SELECT SUBDATE('$currentDate', INTERVAL 1 MONTH) AS prevMonth";

But how can I complete that and apply it in the condition in my php ? Really need a help here. Thanks

I would do this in PHP rather than MySQL:

<?php
// Compare mysql result in Y-m format to input in same format
if (date('Y-m', strtotime($mysqlResult)) < date('Y-m', strtotime($textboxValue)))
    echo "You're in the new month (" . date('m') . ")";
else
    echo "You're still in this month."

Without knowing what your exact requirements are for outputting you're in a new month , this is what I got

$time = strtotime('2015-02-01');
//Change the string literal to your $_POST variable
if((int)date('j',$time)==1){
    echo "The month of ". date('Y-m-01',$time-1)." has passed";
}else{
    echo "Current month, nevermind";
}

Let me know what your requirements are and I can hopefully help you work this out. There really isn't a need for MySQL in this case.

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