4

I have problem to parse the following two time-stamp strings in a generic way?

$timestamp1 = "2013-10-15 19:05:18.756932+03";
$timestamp2 = "2013-10-15 19:05:18.756932+03:00";

I have this so far but it returns with false

$datetime = DateTime::createFromFormat('Y-m-d HH:MM:SS frac "GMT"? [+-] hh ":"? MM?', $timestamp1);
$datetime = DateTime::createFromFormat('Y-m-d HH:MM:SS frac "GMT"? [+-] hh ":"? MM?', $timestamp2);

What's wrong? Thank you.

P.S. I tried this 'Y-m-d HH:MM:SSfrac"GMT"?[+-]hh":"?MM?', this ''Y-m-d HH:MM:SS[0-9]+"GMT"?[+-]hh":"?MM?' and this 'Y-m-d HH:MM:SS [0-9] "GMT"? [+-] hh ":"? MM?' but it always fails.

2
  • What does this correspond to -- 19:05:18.756932+03? Commented Nov 21, 2013 at 12:19
  • @Amal Murali 19:05:18, .756932 is fractional part for a second and +03 is timezone offset. Commented Nov 21, 2013 at 12:21

2 Answers 2

6

Assuming the last part is a timezone offset, Y-m-d H:i:s.ue should work.

If this is posgre's default format you will probably need to handle the case when there are no fractional seconds (2013-10-15 19:05:18+03) with Y-m-d H:i:se.

Sign up to request clarification or add additional context in comments.

Comments

2

You have an error in your format string.

$datetime = DateTime::createFromFormat('Y-m-d H:i:s.uP', $timestamp1);

Here is the documentation for the format string options: PHP: DateTime::createFromFormat Manual

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.