jeeves245 Posted August 12, 2009 Share Posted August 12, 2009 I have a query - "UPDATE 'table' SET 'column' = '$_POST['date']' WHERE ........" But the date is in DD-MM-YYYY format. What's the best way to convert my dates to YYYY-MM-DD format before it goes into the DB? I know how to convert them when they're coming OUT of the DB, just not when they're going IN. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/ Share on other sites More sharing options...
.josh Posted August 12, 2009 Share Posted August 12, 2009 date Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-896167 Share on other sites More sharing options...
jeeves245 Posted August 12, 2009 Author Share Posted August 12, 2009 Thanks. Yeah I know that's the function.. just having trouble using it. Tried this: $timestamp2 = $_POST['date']; $new_date = date("d-m-Y",$timestamp2); But the dates still get saved as 0000-00-00 in the DB. Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-896175 Share on other sites More sharing options...
jeeves245 Posted August 12, 2009 Author Share Posted August 12, 2009 Ohhh, well I got the date in the correct format. It writes to the DB, but as 2036-04-21 which isn't right.. I used this: UPDATE `smail` SET `estimatedDelivery` = '" . date("Y-m-d",strtotime($_POST['estimatedDelivery'][$i])) . "' WHERE `psNumber` = '" . mysql_real_escape_string($_POST['id'][$i]) . "'"; Trial and error seems to be getting me somewhere.. slowly Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-896185 Share on other sites More sharing options...
.josh Posted August 12, 2009 Share Posted August 12, 2009 But the date is in DD-MM-YYYY Are you sure that's what you are getting from the form? Echo out the $_POST var. Alternative to date() would be to explode at the hyphen and then just reorder the elements manually. $string = explode('-',$string); $string = array_reverse($string); $string = implode('-',$string); Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-896187 Share on other sites More sharing options...
jeeves245 Posted August 12, 2009 Author Share Posted August 12, 2009 Yes, the values in $_POST['date'] are definitely 00-00-0000 format. Would this work: $string = explode('-',$_POST['estimatedDelivery']); $string = array_reverse($string); $string = implode('-',$string); Then in the update query substitute "UPDATE `smail` SET `estimatedDelivery` = '" . $_POST['estimatedDelivery'][$i] . "' WHERE ... for "UPDATE `smail` SET `estimatedDelivery` = '" . $string[$i] . "' WHERE ... Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-896190 Share on other sites More sharing options...
jeeves245 Posted August 13, 2009 Author Share Posted August 13, 2009 $arr = explode("-", $_POST['estimatedDelivery']); $date = $arr[2] . '-' . $arr[1] . '-' . $arr[0]; Tried that but got an "--ArrayError" Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897102 Share on other sites More sharing options...
TeNDoLLA Posted August 13, 2009 Share Posted August 13, 2009 There is so many ways of doing this... heres one <?php $oldDate = '30-07-2009'; list($day, $month, $year) = explode('-', $oldDate); $newDate = $year . '-' . $month . '-' . $day; echo $newDate; // 2009-07-30 EDIT: OOpps.. didnt read all messages posted here, this is quite the same as already mentioned... Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897106 Share on other sites More sharing options...
jeeves245 Posted August 13, 2009 Author Share Posted August 13, 2009 Wouldn't that just save one date and not the whole array? Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897109 Share on other sites More sharing options...
TeNDoLLA Posted August 13, 2009 Share Posted August 13, 2009 Sorry.. I am not following you now, thought your problem was converting the date to correct mysql DATE format(?) Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897111 Share on other sites More sharing options...
thebadbad Posted August 13, 2009 Share Posted August 13, 2009 You just have to remember that $_POST['estimatedDelivery'] is an array. Just reformat the date in the for loop from your previous thread: <?php $count = count($_POST['estimatedDelivery']); for ($i = 0; $i < $count; $i++) { $date = implode('-', array_reverse(explode('-', $_POST['estimatedDelivery'][$i]))); $sql2 = "UPDATE `smail` SET `estimatedDelivery` = '" . mysql_real_escape_string($date) . "' WHERE `estimatedDelivery` = '" . mysql_real_escape_string($_POST['id'][$i]) . "'"; mysql_query($sql2); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897113 Share on other sites More sharing options...
jeeves245 Posted August 13, 2009 Author Share Posted August 13, 2009 Hmm.. When I use print_r ($date); to see what is in that array, it only gives me one date even though $_POST['estimatedDelivery'] contains about 20 dates. When I run the code you gave me, no changes are made in the DB at all. Bah, this is like the last line I need to complete the program and i've spent about 2 days on it Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897119 Share on other sites More sharing options...
TeNDoLLA Posted August 13, 2009 Share Posted August 13, 2009 Why dont you do print_r($_POST['estimateDelivery']); and paste the results in here, then we can surely show the right way when we know the contents we are dealing with. Or maybe not the whole content if it is large, but some data from the beginning atleast to describe the structure of the array and the data in it. Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897120 Share on other sites More sharing options...
jeeves245 Posted August 13, 2009 Author Share Posted August 13, 2009 Why dont you do print_r($_POST['estimateDelivery']); and paste the results in here, then we can surely show the right way when we know the contents we are dealing with. Or maybe not the whole content if it is large, but some data from the beginning atleast to describe the structure of the array and the data in it. print_r($_POST['estimateDelivery']); gives me absolutely no output Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897123 Share on other sites More sharing options...
TeNDoLLA Posted August 13, 2009 Share Posted August 13, 2009 Did you copy paste if from this forum? It had a typo in it, not $_POST['estimateDelivery'] but $_POST['estimatedDelivery'] ? Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897124 Share on other sites More sharing options...
jeeves245 Posted August 13, 2009 Author Share Posted August 13, 2009 Wait.. sorry.. spelled it wrong. I get: Array ( [0] => 16-07-2009 [1] => 16-07-2009 [2] => 16-07-2009 [3] => 16-07-2009 [4] => 16-07-2009 [5] => 16-07-2009 [6] => 16-07-2009 ) Which is correct. Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897126 Share on other sites More sharing options...
thebadbad Posted August 13, 2009 Share Posted August 13, 2009 Hmm.. When I use print_r ($date); to see what is in that array, it only gives me one date That's what you'd expect, since $date is derived from a single element of the $_POST['estimatedDelivery'] array, hence the [$i] part. Just make sure you've got no misspelling, and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897141 Share on other sites More sharing options...
jeeves245 Posted August 13, 2009 Author Share Posted August 13, 2009 Fixed, and program complete Had an error in the where clause. Thanks guys, much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897143 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.