简体   繁体   中英

Issue with no of days while adding date in current datetime

I am currently working on a package subscription manager where I have to calculate the expiry date by adding the duration in current date.

Below is my code that does it:

function generateExpiryDate($start_date, $package_duration)
{
    $expires = strtotime(date($start_date)." + $package_duration");
    return date("Y-m-d H:i:s", $expires);
}

$current_date = "2012-10-23 19:12:19";
$duration = "6 Months";
$expiry_date = generateExpiryDate($current_date, $duration);

When I echo $current_date and $expiry_date, it shows 2012-10-23 19:41:14 2013-04-23 19:41:14

Notice it is just adding no of months in the month and doesnt take care of no of days in month like 31 days, 28 days, 29 days or 30 days. I want it to take those things into account.

Please help me to get it right.

$date = date($current_date);// current date
$expiry_date =strtotime(date("Y-m-d", strtotime($date)) . " +6 month"); 

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