2

I want to check if submitted datetime from user is the same as my datetime format with

DateTime::createFromFormat($format, $date)

It was working fine until I changed my time format from Y-m-d H:i:s to Y-m-d\TH:i:s.ZP I want to get timedate like this 2011-11-09T17:11:57.430+05:00 What format should I give to my function? Is the format I used wrong?

2
  • What is your format? I can't read it while it is buried in a wall of text. Commented Dec 10, 2013 at 4:12
  • @RPM I gave my format and sample datetime in my question, this is what I want, I need the right format for 2011-11-09T17:11:57.430+05:00 Commented Dec 10, 2013 at 4:28

2 Answers 2

5

You are almost there. You have used 'Z' where you should have a 'u' for microseconds, so your code should look something like this:-

$dateStr = '2011-11-09T17:11:57.430+05:00';
$date = \DateTime::createFromFormat('Y-m-d\TH:i:s.uP', $dateStr);
var_dump($date);

Output:-

object(DateTime)[1]
  public 'date' => string '2011-11-09 17:11:57' (length=19)
  public 'timezone_type' => int 1
  public 'timezone' => string '+05:00' (length=6)

The format strings accepted by DateTime::createFromFormat() are the same as those accepted by date().

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

Comments

-1

That's an ISO 8601 date, and you can simply use $format = 'c' for that.

2 Comments

it is not working, probably not working with DateTime::createFromFormat
The PHP ISO8601 format is something like '2004-02-12T15:19:21+00:00'; no microseconds.

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.