Convert month and year dates

I have a credit card validity period with a month and a year only entering as a say string 08/17. How can I change this line in format to pass it toAuthorize.net

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

I tried to use strtotime(), but it gives me the current year

echo $date = date('Y-m', strtotime($ccInfo['exp_date']));
+4
source share
2 answers

You should use date_create_from_formatinstead strtotimeto create your date:

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

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

+5
source

You can also write

$eventDate = DateTime::createFromFormat('m/y', '08/17');
echo date_format($eventDate, 'Y-m');
0
source

Source: https://habr.com/ru/post/1655150/


All Articles