baig772 baig772 - 2 months ago 11
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

08/17
, How can I change this string in a format so that I can pass it to
Authorize.net


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


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

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

Answer

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.