British date format parsing
During the PHP London conference (slides are here as usual), a number of British people were "complaining" that PHP's strtotime() and date_create() /new DateTime() functions do not understand the British date format "dd/mm/yyyy". Historically, the "English" text to date parsers in PHP only accept the illogical American format of "mm/dd/yyyy". Having seen this problem before, I recently added a new function/method to PHP's Date/Time support to address exactly this issue. From PHP 5.3 the new date_create_from_format() function and the DateTime::createFromFormat() factory method are available. As first argument they accept the expected format, and as second argument the string to parse. To parse a British date string, you would for example use:
<?php $dt = date_create_from_format( 'd/m/Y', "02/03/2008" ); echo $dt->format( 'd/m/Y' ), "\n"; ?>
In case the passed string does not match the format, the function will return false. With the also new function date_get_last_errors() method you then can request more information about which part of the string could not be passed. The following example illustrates that:
<?php
$dt = date_create_from_format( 'Y-m-d', "02/03/2008" );
if ( !$dt )
{
$errors = date_get_last_errors();
var_dump( $errors['errors']);
}
?>
The $errors contains a list of errors, indexed by the position in the string where the error was found. The above script would produce:
array(3) {
[2]=>
string(22) "Unexpected data found."
[5]=>
string(22) "Unexpected data found."
[8]=>
string(13) "Trailing data"
}
The format specifiers are mostly similar to the ones used for the date() function. Documentation on the new functions should make it into the documentation soon.
Comments
Thats great news.. the strtotime format was always a bit too magical to me =)
Fan-bloody-tastic. Well done old chap!
Thanks Derek!
With a useful title as well, so I can find it again when I google for it!
Rob...
It's not a "British" format, it's the normal format used everywhere. The American format is the odd one out ;)
Hi!
Thank you - being one of those annoying people who pestered you for this, I'm much appreciative of your hard work :)
Thanks! David.
Now if we could just get that pesky $ at the beginning of every variable to be a £ then we'd be talking.
any chance this gets released as a legacy class or in PEAR compat? This would be so damn usefull if it weren't for being only available in 5.3
Anyway, damn nice job derick!
Thanks Derick,
Really useful addition, I've always had to convert the dates manually. i.e
Thanks
@DewiMorgan: Definitely want to avoid adding flags, as there could be many. I wouldn't be able to do that for PHP 5.2 anyway, and PHP 5.3 has a better solution in the form of date_parse().
Can't say for sure, since I've not used it (still on 5.2 here) but date_parse() seems to be "better" only in as much as it has the exact same problem, so would need the exact same fix. From the manual:
"Date in format accepted by strtotime()."
If flags for critical functionality like this are out of the question (other than all the flags that have already been added to other functions, of course) then maybe a config setting, "make the date functions respect my locale"?
@Dewi: Sorry, meant DateTime::createFromFormat (http://no.php.net/manual/en/datetime.createfromformat.php)
Life Line
Created 3 crossings and a waste_basket; Updated a bus_stop and a waste_basket; Deleted a waste_basket
Created an entrance and a staircase entrance
I walked 7.5km in 1h25m21s
Success (well, 80%) with my first real soldering-required project!
I have a board with components that can power three (different) Neopixel shapes in a (prototype) Lego enclosure.
Need to make lots of nice 8x8 pixel art and fonts now though, and get a properly designed Lego box.
The only problem is a wonky connector for my third Neopixel — I'll have to resolder that.
I also have software so that I can change everything through Wi-Fi.
I walked 8.0km in 1h32m34s
Mark roads and tracks as private, as I found out yesterday and got stuck behind a locked fence with a security guard
Updated a bird_hide
Updated a bench
This is not an accessible site. Needs more updates too
This is not an accessible site. Needs more updates too
Created 2 gates
I hiked 18.6km in 4h24m32s
I walked 6.2km in 59m46s
This is indeed no more a clothes shop, but a restaurant
I walked 5.2km in 1h8m43s
Created 2 waste_baskets, 2 boards, and 2 benches; Updated 8 benches, 6 waste_baskets, and 2 other objects; Deleted 2 waste_baskets and a bench
I walked 5.9km in 1h25m05s
I walked 1.1km in 10m15s
Updated a pub
I walked 9.6km in 1h37m31s
Merge branch 'v2022'
Merge branch 'php_gh_19803' into v2022
Fix PHP GH-19803: Parsing a string with a single white space does cre…
Merge branch 'xdebug_3_5'
Back to -dev


Shortlink
This article has a short URL available: https://drck.me/bdfp-68x