0

I am importing a excel , it seems from source the date is coming like this : "2021-04-08T12:36:12+03:00"

i used the function

public function transformDate($value) try { return \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value); } catch (\ErrorException $e) { return '-1'; }

this function did a exception , if i used strtotime(2021-04-08T12:36:12+03:00) , it return bad date like : 0616-05-07 00:05:00

i can't find the correct function

3
  • Have you checked what the exception message says? Commented Apr 12, 2021 at 14:55
  • I get a correct result with strtotime, post your full code: 3v4l.org/GAnrt Commented Apr 12, 2021 at 14:56
  • My advice would be to use the DateTime class. Its constructor can parse this string perfectly. Commented Apr 12, 2021 at 15:24

1 Answer 1

1

The date string "2021-04-08T12:36:12+03:00" contains a time zone +03:00. The best way is to use DateTime to keep the correct time zone.

$dateTime = new DateTime("2021-04-08T12:36:12+03:00");
echo $dateTime->format('Y-m-d H:i:s O');
//2021-04-08 12:36:12 +0300

If the date is required for a different time zone, this can be done with setTimeZone().

$dateTime->setTimeZone(new DateTimeZone("Europe/Berlin"));
echo $dateTime->format('Y-m-d H:i:s e');
//2021-04-08 11:36:12 Europe/Berlin

If we use strtotime and Date then we get a date and a time according to the setting of our server (My time zone is "Europe/Berlin").

echo date('Y-m-d H:i:s e',strtotime("2021-04-08T12:36:12+03:00"));
//2021-04-08 11:36:12 Europe/Berlin
Sign up to request clarification or add additional context in comments.

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.