anthony-needs-you Posted December 17, 2008 Share Posted December 17, 2008 hi i have a date picker, here is my form field: <input name="departureDate" type="text" class="fields" id="departureDateField" value="dd/mm/yy" onfocus="this.select();lcs(this)" onclick="event.cancelBubble=true;this.select();lcs(this)"> I pass this to: <?php if(isset($_POST['save'])) { $departureDate = $_POST['departureDate']; $expireDate = $_POST['expireDate']; $airport = $_POST['airport']; $resort = $_POST['resort']; $hotel = $_POST['hotel']; $duration = $_POST['duration']; $board = $_POST['board']; $price = $_POST['price']; $specialoffer = $_POST['specialoffer']; $description = $_POST['description']; $customerRef = $_POST['customerRef']; $mystiqueRef = $_POST['mystiqueRef']; $stars = $_POST['stars']; if(!get_magic_quotes_gpc()) { $departureDate = addslashes($departureDate); $expireDate = addslashes($expireDate); $airport = addslashes($airport); $resort = addslashes($resort); $hotel = addslashes($hotel); $duration = addslashes($duration); $board = addslashes($board); $price = addslashes($price); $specialoffer = addslashes($specialoffer); $description = addslashes($description); $customerRef = addslashes($customerRef); $mystiqueRef = addslashes($mystiqueRef); $stars = addslashes($stars); } include 'library/config.php'; include 'library/opendb.php'; $query = "INSERT INTO test (departureDate, expireDate, airport, destination, resort, hotel, duration, board, price, specialoffer, description, customerRef, mystiqueRef, stars) VALUES ('$departureDate', '$expireDate', '$airport', '$destination', '$resort', '$hotel', '$duration', '$board', '$price', '$specialoffer', '$description', '$customerRef', '$mystiqueRef', '$stars')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); include 'library/closedb.php'; echo "<div class=\"alert\">Holiday '$mystiqueRef' added</div>"; } ?> my sql is: `departureDate` DATE NULL, but all i get is: 0000-00-00 whatever date i put in can someone please tell me what i've done wrong, thanks Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/ Share on other sites More sharing options...
Mark Baker Posted December 17, 2008 Share Posted December 17, 2008 What datatype are you using for the dates in your test table? Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-717924 Share on other sites More sharing options...
anthony-needs-you Posted December 17, 2008 Author Share Posted December 17, 2008 i've got it set as date Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-717930 Share on other sites More sharing options...
Maq Posted December 17, 2008 Share Posted December 17, 2008 Can you echo out the query to ensure they contain correct values: $query = "INSERT INTO test (departureDate, expireDate, airport, destination, resort, hotel, duration, board, price, specialoffer, description, customerRef, mystiqueRef, stars) VALUES ('$departureDate', '$expireDate', '$airport', '$destination', '$resort', '$hotel', '$duration', '$board', '$price', '$specialoffer', '$description', '$customerRef', '$mystiqueRef', '$stars')"; echo $query; Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-717934 Share on other sites More sharing options...
twm Posted December 17, 2008 Share Posted December 17, 2008 looks like you are asking users to enter the date in dd/mm/yy format, then you are trying to insert it into the DB in the same format. in you database the date field is in yyyy-mm-dd format. maybe you should format the date to mysql format before trying to insert it Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-717939 Share on other sites More sharing options...
anthony-needs-you Posted December 17, 2008 Author Share Posted December 17, 2008 i've echo the date and it shows as 17/12/2008 How do i change this to a mysql format would i change the value in the form? <input name="departureDate" type="text" class="fields" id="departureDateField" value="yy/dd/mm" onfocus="this.select();lcs(this)" onclick="event.cancelBubble=true;this.select();lcs(this)"> Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-717941 Share on other sites More sharing options...
anthony-needs-you Posted December 17, 2008 Author Share Posted December 17, 2008 would this work function format_date_mysql($departureDate){ $output = ''; $temp = explode("-",$departureDate); if($departureDate) { $output = $temp[2]."-".$temp[0]."-".$temp[1]; } return $output; } Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-717958 Share on other sites More sharing options...
twm Posted December 17, 2008 Share Posted December 17, 2008 this could work: <?PHP $date = explode("/",$departureDate); // re-assign to var in mysql format $departureDate = sprintf("%04d-%02d-%02d", $date[2], $date[1], $date[0]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-717982 Share on other sites More sharing options...
jeff5656 Posted December 17, 2008 Share Posted December 17, 2008 Here's how I handle the date. $newdate = date("Y-m-d", strtotime($_POST['departureDate'])); Then I add $newdate into the database: "INSERT INTO test (departureDate, expireDate, ....) VALUES ('$newdate', '$expireDate', '$airport', '$destination', '$resort', '$hotel', '$duration', '$board', '$price', '$specialoffer', '$description', '$customerRef', '$mystiqueRef', '$stars')"; Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-717987 Share on other sites More sharing options...
anthony-needs-you Posted December 17, 2008 Author Share Posted December 17, 2008 cool cheers guys thanks for all your help, its really appreciated Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-718077 Share on other sites More sharing options...
Maq Posted December 17, 2008 Share Posted December 17, 2008 There are multiple ways of handling dates but logically one would use the date function to handle them. It's the safest/easiest way to handle them. Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-718082 Share on other sites More sharing options...
anthony-needs-you Posted December 17, 2008 Author Share Posted December 17, 2008 ok this is what i've got: if(isset($_POST['save'])) { $departureDate = date("Y-m-d", strtotime($_POST['departureDate'])); $expireDate = date("Y-m-d", strtotime($_POST['expireDate'])); $airport = $_POST['airport']; $resort = $_POST['resort']; $hotel = $_POST['hotel']; $duration = $_POST['duration']; $board = $_POST['board']; $price = $_POST['price']; $specialoffer = $_POST['specialoffer']; $description = $_POST['description']; $customerRef = $_POST['customerRef']; $mystiqueRef = $_POST['mystiqueRef']; $stars = $_POST['stars']; if(!get_magic_quotes_gpc()) { $airport = addslashes($airport); $resort = addslashes($resort); $hotel = addslashes($hotel); $duration = addslashes($duration); $board = addslashes($board); $price = addslashes($price); $specialoffer = addslashes($specialoffer); $description = addslashes($description); $customerRef = addslashes($customerRef); $mystiqueRef = addslashes($mystiqueRef); $stars = addslashes($stars); } include 'library/config.php'; include 'library/opendb.php'; $query = "INSERT INTO test (departureDate, expireDate, airport, destination, resort, hotel, duration, board, price, specialoffer, description, customerRef, mystiqueRef, stars) VALUES ('$departureDate', '$expireDate', '$airport', '$destination', '$resort', '$hotel', '$duration', '$board', '$price', '$specialoffer', '$description', '$customerRef', '$mystiqueRef', '$stars')"; echo $query; mysql_query($query) or die('Error, query failed : ' . mysql_error()); include 'library/closedb.php'; echo "<div class=\"alert\">Holiday '$mystiqueRef' added</div>"; } -------------------------------------------------------------------------------------- and this is my form field: <input name="departureDate" type="text" class="fields" id="departureDateField" value="dd/mm/yy" onfocus="this.select();lcs(this)" onclick="event.cancelBubble=true;this.select();lcs(this)"> For some reason it return the wrong date selected, has anyone have a clue whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-718257 Share on other sites More sharing options...
anthony-needs-you Posted December 17, 2008 Author Share Posted December 17, 2008 and also this, it got chopped from the above: $query = "INSERT INTO test (departureDate, expireDate, airport, destination, resort, hotel, duration, board, price, specialoffer, description, customerRef, mystiqueRef, stars) VALUES ('$departureDate', '$expireDate', '$airport', '$destination', '$resort', '$hotel', '$duration', '$board', '$price', '$specialoffer', '$description', '$customerRef', '$mystiqueRef', '$stars')"; echo $query; mysql_query($query) or die('Error, query failed : ' . mysql_error()); include 'library/closedb.php'; echo "<div class=\"alert\">Holiday '$mystiqueRef' added</div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-718284 Share on other sites More sharing options...
anthony-needs-you Posted December 18, 2008 Author Share Posted December 18, 2008 Ignore that last comment, late one. I'm getting a lot of 2009-01-12? When picking 2008-12-25 Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-718396 Share on other sites More sharing options...
twm Posted December 18, 2008 Share Posted December 18, 2008 the way you are using strtotime it works fine if your date is in a format mm/dd/yyyy Quote Link to comment https://forums.phpfreaks.com/topic/137394-dates/#findComment-718408 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.