ShaolinF Posted June 2, 2011 Share Posted June 2, 2011 When using DateTime() create a date from a string I encounter one problem. In the UK the date is structured as dd/mm/yy but the date method only accepts the mm/dd/yy format. How can I change this ? $date = new DateTime('31/05/10'); // fails $date = new DateTime('05/31/10'); // works Quote Link to comment https://forums.phpfreaks.com/topic/238246-changing-format-of-datetime-function/ Share on other sites More sharing options...
requinix Posted June 2, 2011 Share Posted June 2, 2011 D/M/Y won't work, but I think D-M-Y does. Or some other symbol. In PHP 5.3+ you can use DateTime::createFromFormat: $date = DateTime::createFromFormat("31/05/10", "d/m/y"); Otherwise you can break the date apart and reassemble into M/D/Y format: $dateparts = preg_split('/\D+/', "31/05/10"); $date = new DateTime($dateparts[1] . "/" . $dateparts[0] . "/" . $dateparts[2]); Quote Link to comment https://forums.phpfreaks.com/topic/238246-changing-format-of-datetime-function/#findComment-1224349 Share on other sites More sharing options...
jcbones Posted June 2, 2011 Share Posted June 2, 2011 Format accepted by the DateTime class: (sorry about the formatting, forums can mess up columns pretty good.) Description Format Examples American month and day mm "/" dd "5/12", "10/27" American month, day and year mm "/" dd "/" y "12/22/78", "1/17/2006", "1/17/6" Four digit year, month and day with slashes YY "/" mm "/" dd "2008/6/30", "1978/12/22" Four digit year and month (GNU) YY "-" mm "2008-6", "2008-06", "1978-12" Year, month and day with dashes y "-" mm "-" dd "2008-6-30", "78-12-22", "8-6-21" Day, month and four digit year, with dots, tabs or dashes dd [.\t-] mm [.-] YY "30-6-2008", "22.12\t1978" Day, month and two digit year, with dots or tabs dd [.\t] mm "." yy "30.6.08", "22\t12\t78" Day, textual month and year dd ([ \t.-])* m ([ \t.-])* y "30-June 2008", "22DEC78", "14 III 1879" Textual month and four digit year (Day reset to 1) m ([ \t.-])* YY "June 2008", "DEC1978", "March 1879" Four digit year and textual month (Day reset to 1) YY ([ \t.-])* m "2008 June", "1978-XII", "1879.MArCH" Textual month, day and year m ([ .\t-])* dd [,.stndrh\t ]+ y "July 1st, 2008", "April 17, 1790", "May.9,78" Textual month and day m ([ .\t-])* dd [,.stndrh\t ]* "July 1st,", "Apr 17", "May.9" Day and textual month d ([ .\t-])* m "1 July", "17 Apr", "9.May" Month abbreviation, day and year M "-" DD "-" y "May-09-78", "Apr-17-1790" Year, month abbreviation and day y "-" M "-" DD "78-Dec-22", "1814-MAY-17" Year (and just the year) YY "1978", "2008" Textual month (and just the month) m "March", "jun", "DEC" What this tells you is that if you are going to use a 2 digit year, you can separate the day month and year with dots(.) or tabs. Otherwise, you can separate with dots(.),tabs, or dashes(-) if you use a 4 digit year. Quote Link to comment https://forums.phpfreaks.com/topic/238246-changing-format-of-datetime-function/#findComment-1224361 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.