简体   繁体   中英

PHP: add year if missing

I am pulling a date and it only has the month and day. I need to add the year before saving to database. The dates being pulled only go as far a year off. Is there a way to do this?

The way I am receiving the date is Nov 12 or "M d"

Since you said the dates can't be greater than a year ago, then if the date is greater than today counting only month and day, it's from last year. Else, this year.

So the year can be 2010 or 2009 right?

if (strtotime($date . date('-Y')) > time()) {
    // year ago
    $date .= date('-Y', strtotime('last year'));
} else {
    $date .= date('-Y');
}

// date('-Y') will return -2010 or -2009

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