Laravel Cashier DateTime::__construct(): Failed to parse time string (@) at position 0 (@):

I have this code when displaying list of invoices, this is similar or maybe exactly the same to the ones in the official Laravel Cashier documentation. I am getting this weird DateTime::__construct(): Failed to parse time string (@) at position 0 (@): Unexpected character error and I’m not sure what’s the @ character doing in replace of a supposed to be date.

@foreach (Auth::user()->invoices() as $invoice)
    <tr>
        <td>{{ $invoice->date()->toFormattedDateString() }}</td>
        <td>{{ $invoice->total() }}</td>
        <td>
            <a href="/user/invoice/{{ $invoice->id }}" rel="nofollow noreferrer noopener">Download</a>
        </td>
    </tr>
@endforeach

Does anyone have a similar experience? I have also tried to just var_dump each $invoice->date() in the controller but the error is still the same.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

It seems like you’re not using the most upto date version of Laravel Cashier. In older versions, if you follow the code through, you’ll see that Laravel Cashier is trying to format a property that Stripe no longer returns.

cashier\src\Invoice.php, line 48

$carbon = Carbon::createFromTimestampUTC($this->invoice->date);

As per the Stripe “API Upgrade Guide”, you can see on 2019-03-14, they announced the following change;

“The date property has been renamed to created.” (Source: https://stripe.com/docs/upgrades#2019-03-14)

The latest version of Cashier has addressed this issue by checking for the existance of the created property first.

https://github.com/laravel/cashier/blob/9.0/src/Invoice.php#L48

EDIT: If you can’t upgrade for whatever reason, instead of:

$invoice->date()->toFormattedDateString()

You can try something like:

Carbon::createFromTimestamp($invoice->asStripeInvoice()->created)->toFormattedDateString();

Solution 2

Recently I had the same error. The date on the label is null.

I was reading Stripe’s docs and since March 14, 2019 they made some changes.

Laravel Cashier will stop obtaining the invoice date because Stripe will no longer provide it.

There are a few changes to the invoice object:

  • A status_transitions hash now contains the timestamps when an invoice
    was finalized, paid, marked uncollectible, or voided.
  • The date property has been renamed to created.
  • The finalized_at property has been moved into the status_transitions hash.

Now, how did I solve it?

  1. I changed my version of Laravel Cashier by 9.3 in composer.json file.
  2. I opened my terminal and ran composer update.

I hope my solution is useful for you. Regards!

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply