Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dave Wize's avatar

Carbon error data missing

I'm getting an error Data missing on this code

Carbon::createFromFormat('my', $this->exp_date)->lt(now()->startOfMonth()) ? 'Expired' : 'OK';

Worth to mention that the exporation date doesn't have the / between the month and year

0 likes
3 replies
LaryAI's avatar
Level 58

The error "Data missing" is being thrown because the format 'my' is expecting a string with both month and year values, separated by a delimiter. Since the expiration date doesn't have a delimiter, Carbon is unable to parse it correctly.

To fix this, you can manually add the delimiter to the expiration date string before passing it to Carbon. For example, if the expiration date is in the format 'mY' (month and year without delimiter), you can add the delimiter like this:

$exp_date = '0622'; // example expiration date without delimiter
$exp_date_with_delimiter = substr($exp_date, 0, 2) . '/' . substr($exp_date, 2);
// $exp_date_with_delimiter is now '06/22'

Carbon::createFromFormat('m/Y', $exp_date_with_delimiter)->lt(now()->startOfMonth()) ? 'Expired' : 'OK';

This code adds a '/' delimiter between the first two characters (month) and the last two characters (year) of the expiration date string. Then, it passes the formatted string to Carbon using the 'm/Y' format, which expects a string with month and year values separated by a '/' delimiter.

Snapey's avatar
Snapey
Best Answer
Level 122
Carbon::createFromFormat('dmy', '01' . $this->exp_date)->lt(now()->startOfMonth()) ? 'Expired' : 'OK';
Dave Wize's avatar

@Snapey Thank you so much. This is such a simple solution.

And for those who asked why I'm processing payment myself, the answer is that I'm not. It is meant to check if a payment method (via token) is still active.

Please or to participate in this conversation.