refiking Posted August 7, 2013 Share Posted August 7, 2013 I can't figure this out for anything! I'm trying to convert a date (m-d-Y) to (Y-m-d H:i:s) prior to db entry. Here's the code: $date_added = date('Y-m-d 00:00:00', strtotime($_POST['date_added'])); echo $date_added; echo'<hr>'; echo $_POST['date_added']; exit; Link to comment https://forums.phpfreaks.com/topic/280933-date-formatting-problem/ Share on other sites More sharing options...
requinix Posted August 7, 2013 Share Posted August 7, 2013 And the problem is...? strtotime() doesn't accept m-d-Y format. If you know that's the format of the input then you need to get the pieces yourself. if (preg_match('/^(\d\d?)-(\d\d?)-(\d\d\d?\d?)$/', $_POST["date_added"], $matches) && checkdate((int)$matches[1], (int)$matches[2], (int)$matches[3]) { // assume it's m-d-Y format $date = mktime(0, 0, 0, (int)$matches[1], (int)$matches[2], (int)$matches[3]); } else { $date = strtotime($_POST["date_added"]); } Or if you don't mind using m/d/Y format (ie, with slashes instead of hyphens) that will work. Even better: unless you need lots of flexibility with dates, give the user a datepicker-type widget and you can always get a standard format (like Y-m-d) back from your form. Link to comment https://forums.phpfreaks.com/topic/280933-date-formatting-problem/#findComment-1443907 Share on other sites More sharing options...
refiking Posted August 7, 2013 Author Share Posted August 7, 2013 I'd figured out the issue (probably while you were posting). Had to str_replace the slashes.....sheesh I hate missing the ones staring me in the face! Link to comment https://forums.phpfreaks.com/topic/280933-date-formatting-problem/#findComment-1443910 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.