bulrush Posted May 21, 2010 Share Posted May 21, 2010 I have a variable where the user types in a date, like "5/10/2010". I want to store this in a Mysql field that is a DATE type. How do I convert it? When I tried storing the date as a string, the field just contains the default value which is '00-00-0000'. When I tried to convert the date like this: $q="INSERT INTO table (mdate) VALUES ('".strtotime($datevar)."')"; Mysql still stores the default value of '00-00-0000'. The documentation on 2 sites wasn't clear how to make this happen. Thanks. Link to comment https://forums.phpfreaks.com/topic/202519-store-date-string-in-mysql-date-field-type/ Share on other sites More sharing options...
premiso Posted May 21, 2010 Share Posted May 21, 2010 Why not just try and replace the / with -'s using str_replace. That or have them as seperate fields and then combinte them into a string on input. Strtotime does not work as it creates a UNIX Timestamp, if it is passed a string it can handle / convert. Link to comment https://forums.phpfreaks.com/topic/202519-store-date-string-in-mysql-date-field-type/#findComment-1061665 Share on other sites More sharing options...
kenrbnsn Posted May 21, 2010 Share Posted May 21, 2010 Actually the default value for a DATE field in MySQL is 0000-00-00 (YYYY-MM-DD). The strtotime function returns the number of seconds since 1970-01-01. You need to use the date function to format it correctly: <?php $q="INSERT INTO table (mdate) VALUES ('". date('Y-m-d',strtotime($datevar)) ."')"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/202519-store-date-string-in-mysql-date-field-type/#findComment-1061671 Share on other sites More sharing options...
bulrush Posted May 21, 2010 Author Share Posted May 21, 2010 Thank you, Ken. That worked. Link to comment https://forums.phpfreaks.com/topic/202519-store-date-string-in-mysql-date-field-type/#findComment-1061695 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.