farahZ Posted May 16, 2013 Share Posted May 16, 2013 hello, i'm sending date to mysql. when echoing the date in the browser, the date is correctly viewed. $date=date("m.d.y"); echo $date; but when sending to the database, its viewing there as 0's is it a problem in the table in the db or the code itself $sql = "INSERT INTO fooddiary (ID, Date, DayTime, FoodName, Calories) VALUES ($clid, '$date', 'Breakfast', '$food', $cal) ON DUPLICATE KEY UPDATE FoodName = '$food', Calories=$cal"; $result = mysqli_query($con, $sql) or die(mysqli_error()); other values are sent successfully!! Quote Link to comment https://forums.phpfreaks.com/topic/278053-date-is-sent-00000000-to-mysql/ Share on other sites More sharing options...
jugesh Posted May 16, 2013 Share Posted May 16, 2013 Mysql takes date as YYYY-MM-DD format, If your date column is Date type If you want to insert date in date("m.d.y") format make date column as varchar Quote Link to comment https://forums.phpfreaks.com/topic/278053-date-is-sent-00000000-to-mysql/#findComment-1430369 Share on other sites More sharing options...
QuickOldCar Posted May 16, 2013 Share Posted May 16, 2013 You can use datetime in mysql with a structure like 0000-00-00 00:00:00 $date = date('Y-m-d H:i:s'); Quote Link to comment https://forums.phpfreaks.com/topic/278053-date-is-sent-00000000-to-mysql/#findComment-1430374 Share on other sites More sharing options...
farahZ Posted May 16, 2013 Author Share Posted May 16, 2013 switching to varchar solved it.. thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/278053-date-is-sent-00000000-to-mysql/#findComment-1430383 Share on other sites More sharing options...
DavidAM Posted May 16, 2013 Share Posted May 16, 2013 Do NOT store dates as VARCHAR. You will not be able to use the mySql date functions, you will have problems with sorting and comparing, etc. etc. Always store dates as DATE (or DATETIME or even TIMESTAMP). The server accepts and returns them as YYYY-MM-DD. You can change the way it displays when you retrieve the data. Would you store numbers as "two hundred fifty-seven and 23/100" just so you can print it on a check? Of course not. Quote Link to comment https://forums.phpfreaks.com/topic/278053-date-is-sent-00000000-to-mysql/#findComment-1430397 Share on other sites More sharing options...
farahZ Posted May 16, 2013 Author Share Posted May 16, 2013 Hey if I kept it of date type in the db How can I send it using php code? Sure I will use it later for comparison Quote Link to comment https://forums.phpfreaks.com/topic/278053-date-is-sent-00000000-to-mysql/#findComment-1430400 Share on other sites More sharing options...
Barand Posted May 16, 2013 Share Posted May 16, 2013 (edited) $date = date('Y-m-d'); or better still, use CURDATE() in the query instead of '$date' Edited May 16, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/278053-date-is-sent-00000000-to-mysql/#findComment-1430450 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.