baig772 baig772 - 1 month ago 4x
PHP Question

Month and Year date conversion

I have a Credit card expiry date with month and year only coming in form of a string say

, How can I change this string in a format so that I can pass it to

$creditCard->setExpirationDate( "2017-08");

I have tried to use
but it is giving me the current year

echo $date = date('Y-m', strtotime($ccInfo['exp_date']));


You should use date_create_from_format instead of strtotime to build your date:

echo date_create_from_format('m/y', '08/17')->format('Y-m');

The function creates a \DateTime object, so you can call format to get the format you want.