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

fbc's avatar
Level 2

How to change "06/01/2018 01" to Epoch time?

I am importing CSV data and the date/time field is formatted kinda weird and come in as:

06/01/2018 01
06/01/2018 02
06/01/2018 03
...

I would like to save this to a date field in a DB in Unix(Epoch)Time format.

Each ends up being $data[0] on the CSV import.

I tried something like:

                $date = date_create($data [0]);
                $reading->time = (int)date_format($date, 'U');

But it does not really work.

The last two digits are the hour. So this is hourly. I have CARBON installed if that is any help.

Thank you in advanced!

0 likes
12 replies
mabdullahsari's avatar
Level 16

The easiest solution is using createFromFormat and then calling timestamp

e.g. (assuming it is day/month/year)

Carbon\Carbon::createFromFormat('d/m/Y H', $data[0])->timestamp
1 like
Nakov's avatar

@mabdullahsari you are right, sorry for that, deleted my replies, not to confuse someone else.

1 like
jlrdw's avatar

I am guessing you use explode to get the parts.

I usually just:

$tdate = new \DateTime($array[$i][2]);
$ndate = $tdate->format('Y-m-d');

But in your case just use a php string function to remove the trailing 01, 02, etc.

Note, if you want I can post the entire routine (function) I use to import, just let me know, but really removing the stray part shouldn't be that hard.

fbc's avatar
Level 2

I need it. It displays the hour.

Cronix's avatar

Solution by @mabdullahsari should work then. You need to specify the format that the date string is in like he showed in order to parse it to a real date/time that can then be manipulated. It needs to understand the incoming format, which is not normal in your case.

1 like
fbc's avatar
Level 2

yup gonna give that a try soon..thanks!!!!

fbc's avatar
Level 2

something is not right, I'm getting times into 2021 maybe I should be using:

Carbon::createFromFormat('dd/mm/YYYY HH', $data[0])->timestamp

@mabdullahsari @Cronix

Cronix's avatar

works fine here for the format you specified in the first post.

In tinker:

>>> Carbon\Carbon::createFromFormat('d/m/Y H', '06/01/2018 01')
=> Carbon\Carbon @1515200400 {#3238
     date: 2018-01-06 01:00:00.0 UTC (+00:00),
   }

>>> Carbon\Carbon::createFromFormat('d/m/Y H', '06/01/2018 02')
=> Carbon\Carbon @1515204000 {#3218
     date: 2018-01-06 02:00:00.0 UTC (+00:00),
   }

Not sure what your issue is, but the dates you supplied in your original post work fine with that format.

06/01/2018 01
06/01/2018 02
06/01/2018 03

Assuming 06 is the 2 digit day, 01 is the 2 digit month, 2018 is the year and 01 is a 2 digit hour.

Nakov's avatar

@fbc you can debug the input data that you are passing into createFromFormat method using dd($data[0]) above the line where you use it. Ans either pass the result here if you need help or make sure that what you get is trully day/month/year hour format which translates into the format that @mabdullahsari shared above ie d/m/Y H.

Cronix's avatar

Also, take a look at what all of that means in the php date function

dd/mm/YYYY HH'

https://www.php.net/manual/en/function.date.php

For example, a lower case d is a 2 digit day (adds 0's to the front of the number if it's single digit). You have 2 of them, so how would you have a 4 digit day, or 2 separate 2 digit days crammed into one?

Don't confuse the php date placeholders with javascript placeholders. The are not the same thing in both languages and can mean totally different things.

fbc's avatar
Level 2

I ended up having to use this because several lines had blank spaces before the dates:

            $reading->time = Carbon::createFromFormat('d/m/Y H', ltrim($data[18]))->timestamp;

fbc's avatar
Level 2

@cronix @mabdullahsari Thanks alot guys!! I found my problem.. the time in the H column was in 12h and not in 24hr... I'm pretty sure that was what was causing my weird date and chart readings. Thanks.

Please or to participate in this conversation.