3

I've run into a small programming problem. I need to do some time calculations, but as an input I only have a string - so basically I'll need to convert this string to a date/time object in PHP. It's the conversion I'm having trouble with.

$endtimestamp = "September 1, 2012 13:00 PM";

$doesntwork = strtotime(trim($endtimestamp));
$doesntwork2 = date_create_from_format("l, F j, Y G:i A", $endtimestamp);
$doesntwork3 = date("l, F j, Y G:i A", strtotime($endtimestamp));

To be quite honest, I have no idea why these functions don't work as they are supposed to. SO far, other online resources have been vague at best. Can anyone tell me what I'm doing wrong?

2
  • 2
    My guess is that 13:00 PM doesn't make any sense. It's either 1:00 PM or 13:00. Commented Aug 28, 2012 at 22:04
  • Yes, I have noticed this anomaly. Unfortunately, I have no control over the input provided. Commented Aug 28, 2012 at 22:11

2 Answers 2

3

That happens because you have some strange l, in the beginning

If you use this format:

'F j, Y G:i A'

it works fine

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

Comments

2

There are 2 problems here.

One is that 13:00 PM doesn't make any sense. It's either 1:00 PM or 13:00.

strtotime("September 1, 2012 13:00");

Also your format l, F j, Y G:i A doesn't match your input string.

Either the format should be: F j, Y G:i A, or the string should be Saturday, September 1, 2012 13:00 PM.

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.