4

I have an Excel file, where I have a date column. The date looks like "17.07.2020".

I'm using Laravel and Maatwebsite Excel import. When I'm saving the imported data to MySQL, the date in MySQL is always "1970-01-01".

Here is the code, which I have now:

return new Product([
    .......
    'discountBeginning'  => date('Y-m-d', strtotime($row['discount_beginning'])
]);

I would like to format the date from Excel to "2020-07-17".

4
  • I made an answer, but read your question again. How is discountBeginning being stored in your database? Commented Jul 17, 2020 at 11:20
  • To be able to answer this definitely, you need to provide the output of $row['discount_beginning']? Looks like the strtotime() call is the bug here, since it is just returning the Unix Epoch Time (1970-01-01). Perhaps $row['discount_beginning']contains a format that strtotime() is unable to parse? Commented Jul 17, 2020 at 11:26
  • The output in the database is always "1970-01-01", even though the date in Excel is 17.07.2020 Commented Jul 17, 2020 at 11:42
  • I also got that 1970-01-01 date. What really worked for me is $your_date = PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['your_date']); from the comment section of this question. In the excel row, I got a string as 2022-05-29 (for example) and that worked! Commented Aug 19, 2022 at 17:38

4 Answers 4

3

Try this

$date = new DateTime($row['discount_beginning']);
echo $date->format('d.m.Y');
Sign up to request clarification or add additional context in comments.

4 Comments

I dont think this will work given the format in OPs excel file. ie) 6.5.2020 is it May 6th, or June 5th?
I am wrong DateTime has the same interpretation, d.m.Y.
Okay i think my updated answer is work for @Jakub Kříž
I get this error: Failed to parse time string (44710) at position 4 (0)
0

You are using Laravel, so you already have access to the awesome Carbon library. For how data is formatted in your excel file (day.month.year), you can use it like:

return new Product([
    .......
    'discountBeginning'  => \Carbon\Carbon::createFromFormat('d.m.Y', $row['discount_beginning']),
]);

Once you have the carbon instance, you can format it however you would like according to php's date syntax like:

$date = \Carbon\Carbon::createFromFormat('d.m.Y', $row['discount_beginning']);
$date->format('Y-m-d');

7 Comments

I've tried your code, but it's returning an error "The separation symbol could not be found"
I'm using Excel's default date format, which is exactly "17.07.2020"
It won't even let me dd somehow. Always the same error "The separation symbol could not be found".
Well, it looks like its grabbing the date in General format, where 17.07.2020 looks like 44029 in Excel, even though column format is date.
Yes, the output in log was 44029. It's one of the formats in Excel, like for example Time, Currency, Date, Percentage etc. bettersolutions.com/excel/formatting/…
|
0
$date =  "17.07.2020";
$d2 = \Carbon\Carbon::createFromFormat('d.m.Y', $date)->format('Y-m-d');
dd($d2); // returns: "2020-07-17"

you can also try to append ->toDateString(); if your returned date is not a string. If it is already a string, this will fail.

2 Comments

What if I get the The separation symbol could not be found Data missing error?
-1

Try this. it will work sure. I tested with my code but note that the imported file should be in CSV format...

  Carbon::createFromFormat('d/m/Y', $row['discount_beginning'])->format('Y-m-d')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.