简体   繁体   中英

Comparing month and date to current date

I am trying to compare month and year values from data in a database to the current date. I am running a query to get the month and year integer values(eg: month-> 5 year-> 2017):

$data = $wpdb->get_row("SELECT month, year FROM $table WHERE id='$id'");
$month = date('m', $data->month);
$year = date('Y', $data->year);
$dateFromTable = $year.$month;

Then I want to compare them to the current month and date:

$current = date('Ym');
if((int)$dateFromTable >= (int)$current){
    //...do something
}

I am not sure how I can go about combining the month and year values from the table so I can make my compare against the current month and year. Any help would be greatly appreciated.

Something like this.

$date = DateTime::createFromFormat('mY', $data->month.$data->year);
$curdate = new DateTime(date('mY'));

$diff = $curdate->diff($date);
echo('days:' .$diff->days);
echo('months:' .$diff->m); 

DateInterval

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