Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

php - How to make strtotime parse dates in Australian (i.e. UK) format: dd/mm/yyyy?

I can't beleive I've never come across this one before.

Basically, I'm parsing the text in human-created text documents and one of the fields I need to parse is a date and time. Because I'm in Australia, dates are formatted like dd/mm/yyyy but strtotime only wants to parse it as a US formatted date. Also, exploding by / isn't going to work because, as I mentioned, these documents are hand-typed and some of them take the form of d M yy.

I've tried multiple combinations of setlocale but no matter what I try, the language is always set to US English.

I'm fairly sure setlocale is the key here, but I don't seem to be able to strike upon the right code. Tried these:

  • au
  • au-en
  • en_AU
  • australia
  • aus

Anything else I can try?

For clarity: I'm running on IIS with a Windows box.

Thanks so much :)

Iain

Example:

$mydatetime = strtotime("9/02/10 2.00PM");
echo date('j F Y H:i', $mydatetime);

Produces

2 September 2010 14:00

I want it to produce:

9 February 2010 14:00

My solution

I'm giving the tick to one of the answers here as it is a much easier-to-read solution to mine, but here's what I've come up with:

$DateTime = "9/02/10 2.00PM";
$USDateTime = preg_replace('%([0-3]?[0-9]{1})s*?[./ ]s*?((?:1[0-2])|0?[0-9])s*?[./ ]s*?(d{4}|d{2})%', '${2}/${1}/${3}', $DateTime);  
echo date('j F Y H:i',strtotime($USDateTime));

Because I can't rely on users to be consistent with their date entry, I've made my regex a bit more complex:

  • 0 or 1 digit between 0 and 3
  • 1 digit between 0 and 9 -- yes this will match 37 as a valid date but I think the regex is already big enough!
  • Could be some whitespace
  • Delimiting character (a '.', a '/' or a ' ')
  • Could be some whitespace
  • Either:
    • A number between 10 and 12 OR
    • A number between 1 and 9 with an optional leading 0
  • Could be some whitespace
  • Delimiting character (a '.', a '/' or a ' ')
  • Could be some whitespace
  • Either:
    • A number 2 digits long OR
    • A number 4 digits long

Hopefully this will match most styles of date writing...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There's a quick fix that seems to force PHP's strtotime into using the UK date format. That is: to replace all of the '/' in the incoming string with a '-'.

Example:

date('Y-m-d', strtotime('01/04/2011'));

Would produce: 2011-01-04

date('Y-m-d', strtotime('01-04-2011'));

Would produce: 2011-04-01

You could use str_replace to achieve this.

Example:

str_replace('/', '-', '01/04/2011');

EDIT:

As stated in the comments, this works because PHP interprets slashes as American and dots/dashes as European.

I've used this trick extensively and had no problems so far.

If anyone has any good reasons not to use this, please comment.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...