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

Asked
Active3 hr before
Viewed126 times

5 Answers

datesparse
90%

@VladimirSerykh Read the question again. It clearly states "dates are formatted like dd/mm/yyyy" meaning the questioner's dates were formatted using slashes but the order was wrong. I suggested to convert those slashes into dashes which corrects the order. – Chris Harrison Oct 2 '15 at 1:20 ,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., 1 Manipulating strings is not the best solution to get correct date. It's conceptually wrong. You'd better use date_parse_from_format ($format , $date ) or DateTime::createFromFormat($format , $date) I think. – Vladimir Serykh Oct 3 '15 at 17:02 ,For future readers, try the following for parsing dates from unstrusted user input, for which strptime() is too rigid. It outputs ISO date strings which you can pass to new Date(), strtotime(), etc., or false for invalid dates, or null for no usable input. You'd have to adapt it to process times. It also accepts 2-digit year and ISO date input.

Example:

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

Would produce: 2011-01-04

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

Example:

str_replace('/', '-', '01/04/2011');
load more v
88%

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.,The problem is that strtotime doesn't take a format argument. What about strptime?,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 '-'.,Though this relies on dates in this format dd/mm/yyyy.

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

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));
load more v
72%

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.,The behavior is correct. Both date intervals represent same number of days (364). The month and date part represent the period that needs to be added to/subtracted from first date to reach second date:,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 '-'.,Because I can't rely on users to be consistent with their date entry, I've made my regex a bit more complex:

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

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));
load more v
65%

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in baseTimestamp, or the current time if baseTimestamp is not supplied. , The Unix timestamp that this function returns does not contain information about time zones. In order to do calculations with date/time information, you should use the more capable DateTimeImmutable. ,strtotime — Parse about any English textual datetime description into a Unix timestamp, For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.

load more v
75%

To use the JsonRestStore your response would have to be only the items part of the sample you gave:,Instead use the Response.StatusCode and Response.StatusDescription to return meaningful information to the client.,Answering to your edit, the way I'd do it would be to in case of a file not found on the server side would return a 404 Status Code. And let the client handle this.,Are you sure it shouldn't be:

To use the JsonRestStore your response would have to be only the items part of the sample you gave:

[{
   "name": "My Super Cool Collab",
   "description": "This is for all the super cool people in the super cool group",
   "id": 1
}, {
   "name": "My Other Super Cool",
   "description": "This is for all the other super cool people",
   "id": 3
}, {
   "name": "This is another coll",
   "description": "This is just some other collab",
   "id": 4
}, {
   "name": "some new collab",
   "description": "this is a new collab",
   "id": 5
}, {
   "name": "yet another new coll",
   "description": "uh huh",
   "id": 6
}, {
   "name": "asdf",
   "description": "asdf",
   "id": 7
}]
load more v

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