0

This should be simple, but I'm having trouble... so turn to StackOverflow...

I'm in the UK and am getting a date from the jQuery DatePicker in a dd/mm/yy format. I want to store this date as a serial (yyyymmdd) so run a Date_To_Serial function that just does:

return date("Ymd", strtotime($strDate_In));

where $strDate_In is the date string in dd/mm/yy format.

So, passing in 01/12/2013, I expect 20131201 to be returned.

But, when 01/12/2013 is passed in, it returns 20130112 - PHP obviously assumes the date format is mm/dd/yyyy.

How can I fix this please?

3
  • So what is the problem? Commented Jan 24, 2014 at 11:20
  • @JezB : Hello friend, date picker provide to set the format of date. So if you choose Ymd there then i think you will not get the problem again. Try it Commented Jan 24, 2014 at 11:27
  • @JezB : Please see jqueryui.com/resources/demos/datepicker/date-formats.html Commented Jan 24, 2014 at 11:32

4 Answers 4

5

If you know the format, try using the static createFromFormat method on the DateTime class:

$date = DateTime::createFromFormat('d/m/Y', '01/12/2013');
return $date->format('Ymd');
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - I knew someone would know very quickly!
1

If the separator is a / then strtotime assumes a US-format. To have it recognise a UK format the separator must be - or .:

echo date('Ymd', strtotime('01/12/2014')); // 20140112
echo date('Ymd', strtotime('01-12-2014')); // 20141201

So for this to work in your example you would do:

return date("Ymd", strtotime(str_replace('/', '-', $strDate_In)));

1 Comment

"f the separator is a / then strtotime assumes a US-format. To have it recognise a UK format the separator must be - or .:" - I didn't know that... thanks!
0

Use a DateTime object's createFromFormat method. It allows you to specify the format of the input.

Afterwards, use the format method to export the date in the desired format.

Comments

0

Check out DateTime::createFromFormat for correct handling of non-standard date formats.

return DateTime::createFromFormat("d/m/Y", $strDate_In)->format("Ymd");

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.