baig772 baig772 - 1 year ago 95
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']));

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download