johnrb87 Posted July 25, 2010 Share Posted July 25, 2010 Hi there I wonder if anyone can help. I have a HTML form which i'm posting the contents of to a PHP file At the moment, one of the fields i'm posting is called `date` and the value looks like Jul 14, 2010 At in my PHP code I have <?php $date = $_POST['date']; ?> is it possible to get PHP to convert the date format Jul 14, 2010 to 2010-07-14 Any help would be great as im really struggling to do this basic task Thanks John Quote Link to comment https://forums.phpfreaks.com/topic/208855-formatting-date/ Share on other sites More sharing options...
AbraCadaver Posted July 25, 2010 Share Posted July 25, 2010 $date = date('Y-m-d', strtotime($_POST['date'])); Quote Link to comment https://forums.phpfreaks.com/topic/208855-formatting-date/#findComment-1090994 Share on other sites More sharing options...
BillyBoB Posted July 26, 2010 Share Posted July 26, 2010 After some testing I have come up with a pretty basic way of doing it. The only reason i didn't do it completely with substr is: the day could be entered without the starting zero. It does a check for that with strlen and fixes that problem. <?php $months = array("Jan" => '01', "Feb" => '02', "Mar" => '03', "Apr" => '04', "May" => '05', "Jun" => '06', "Jul" => '07', "Aug" => '08', "Sep" => '09', "Oct" => '10', "Nov" => '11', "Dec" => '12'); $date = "Jul 4, 2010"; $year = substr($date, -4); $month = substr($date, 0, 3); preg_match("/ (\d{1,2}),/", $date, $matches); $day = $matches[1]; if(strlen($day)==1) $day = "0".$day; $date = $year.'-'.$months[$month].'-'.$day; echo $date; ?> Good luck with the rest of your code. Quote Link to comment https://forums.phpfreaks.com/topic/208855-formatting-date/#findComment-1091156 Share on other sites More sharing options...
AbraCadaver Posted July 26, 2010 Share Posted July 26, 2010 I'm just curious why you would go through all of that when one strtotime() call does it? Quote Link to comment https://forums.phpfreaks.com/topic/208855-formatting-date/#findComment-1091285 Share on other sites More sharing options...
Jessica Posted July 26, 2010 Share Posted July 26, 2010 I'm just curious why you would go through all of that when one strtotime() call does it? Seriously....PHP has this awesome manual.... :-P Quote Link to comment https://forums.phpfreaks.com/topic/208855-formatting-date/#findComment-1091309 Share on other sites More sharing options...
Maq Posted July 26, 2010 Share Posted July 26, 2010 I'm just curious why you would go through all of that when one strtotime() call does it? For fun...? Quote Link to comment https://forums.phpfreaks.com/topic/208855-formatting-date/#findComment-1091317 Share on other sites More sharing options...
.josh Posted July 26, 2010 Share Posted July 26, 2010 omg it's jesi! Quote Link to comment https://forums.phpfreaks.com/topic/208855-formatting-date/#findComment-1091321 Share on other sites More sharing options...
Jessica Posted July 27, 2010 Share Posted July 27, 2010 omg it's jesi! OMG NO WAI!!! Quote Link to comment https://forums.phpfreaks.com/topic/208855-formatting-date/#findComment-1091675 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.