stapler Posted February 15, 2008 Share Posted February 15, 2008 searched for a while but can't find out where to configure this...I want to convert a date string like "2-15-08" to a standardized "mm/dd/yyyy" format, i.e. "02/15/2008", so I thought I could do date("m/d/Y", strtotime("2-15-08")) but for some reason strtotime() is only working for me if it's a european date style, i.e. "15-2-08"...my time zone is properly set to central time, and, if it's at all relevant, date_ default_ timezone_ get() returns "American/Chicago" so it knows I'm in the US....what n00b thing am I missing here? thanks. Link to comment https://forums.phpfreaks.com/topic/91263-strtotime-wont-work-with-american-style-dates/ Share on other sites More sharing options...
cooldude832 Posted February 15, 2008 Share Posted February 15, 2008 strtotime is very fussy about its stuff if your date is consitently in m/dd/yy you can try treating it as a string, exploding it at the "/" and then reforming into a tiemstamp with the mktime() function. strtotime should really only be used for its "+1 day", "-1 year" ability to adjust times and minimal other cases. Link to comment https://forums.phpfreaks.com/topic/91263-strtotime-wont-work-with-american-style-dates/#findComment-467708 Share on other sites More sharing options...
stapler Posted February 15, 2008 Author Share Posted February 15, 2008 thanks, it looks like I should go that route. I also discovered it wasn't an american/european thing, since it works fine with "2-15-2008", so it's the 2-digit year it doesn't like... Link to comment https://forums.phpfreaks.com/topic/91263-strtotime-wont-work-with-american-style-dates/#findComment-467711 Share on other sites More sharing options...
cooldude832 Posted February 15, 2008 Share Posted February 15, 2008 there is a lotta stuff it doesn't like which is why I basically treated it as the blacksheep to the php date family. mktime is so much easier + you can always check consitentcy of values like saying <?php $date = "12/2/2008"; list($month,$day,$year) = explode("/",$date); if(intval($month) < 1 || intval($month) >12){ #invalid month } if(intval($day) <28 || intval($day) >31){ #invalid day } Link to comment https://forums.phpfreaks.com/topic/91263-strtotime-wont-work-with-american-style-dates/#findComment-467713 Share on other sites More sharing options...
kenrbnsn Posted February 15, 2008 Share Posted February 15, 2008 When strtotime() sees a string with dashes between the numbers it assumes it's in the format of yyyy-mm-dd or yy-mm-dd 2-15-2008 doesn't work, when it test with <?php echo date('m/d/Y',strtotime($argv[1])) . "\n"; ?> Using the CLI version, I get $ php -q -f testdt.php 2008-02-15 02/15/2008 $ php -q -f testdt.php 15-02-08 02/08/2015 $ php -q -f testdt.php 2-15-2008 12/31/1969 $ php -q -f testdt.php 08-08-08 08/08/2008 $ php -q -f testdt.php 08-07-06 07/06/2008 $ php -q -f testdt.php 2-15-2008 12/31/1969 Ken Link to comment https://forums.phpfreaks.com/topic/91263-strtotime-wont-work-with-american-style-dates/#findComment-467724 Share on other sites More sharing options...
stapler Posted February 15, 2008 Author Share Posted February 15, 2008 thanks for illuminating this. I went with the mktime function and it works exactly as desired now Link to comment https://forums.phpfreaks.com/topic/91263-strtotime-wont-work-with-american-style-dates/#findComment-467742 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.