Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
Carbon create from timestamp

I am trying to convert some dates

<p>{{  Carbon\Carbon::parse($post->created_at)->diffForHumans()  }}</p>

This works fine as the date is in the format 2017-05-01 10:52:51

However I also have the dates stored as unix timestamps.

How can I covert these to a date?

I already tried

{{  Carbon\Carbon::createFromTimestamp($post->created_at)->toDateTimeString()  }}

Error - A non well formed numeric value encountered

The dump is

 <pre class="xdebug-var-dump" dir="ltr">
<small>int</small> 
<font color="#4e9a06">1493637826</font>
    </pre>
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your first attempt using createFromTimestamp() is correct, so there must be something wrong with your input. Here is a working example:

>>> \Carbon\Carbon::createFromTimestamp(1493637826)->toDateTimeString();
=> "2017-05-01 11:23:46"

There error you encountered suggests you're passing in a DateTime string, not a unix timestamp:

>>> \Carbon\Carbon::createFromTimestamp('2017-05-01 11:23:46')->toDateTimeString();
PHP Notice:  A non well formed numeric value encountered
about 4 years ago · Santiago Trujillo Report

0

In the end I had to use gmdate to convert the timestamp.

{{  Carbon\Carbon::parse(gmdate("Y-m-d\TH:i:s", $post->date_posted))->diffForHumans()  }}

It is strange because it should work the way Robbie Averill suggested.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!