Tiruak Posted June 19, 2010 Share Posted June 19, 2010 Hello ladies and gals I'm very new to php, only a couple months working with it, and am having some difficult to do a couple of things. My question is probably very stupid, and there are probably some obvious answers to it, but I really appreciate if you can help me get to the solution, even if you have to call me "dumb" after you do it After completing a form, an user has a field to insert a date of birth, in a format dd/mm/yyyy (eg 23/06/1980). When I try to add this to the database, in a field that expects a date, it actually just gives me 0000-00-00 as a result. Now, I understand that the sql database is expecting the date to be passed in a different format, so I tried to use the following command on my date: $date="23/06/1980"; // this variable is just for testing purpose, I actually get the date with $_POST $new_date=date('Y-m-d',strtotime($date)); Now if I echo $new_date, it returns me 1980-23-06, because the strtotime() function expects a date in US English format (m/d/Y). So, I assume the answer is probably simple, but I can't get to format the date in the way I need, to be able to add it to the database and store it correctly. Of course, when I read it back and format to the user to read, I will need to reverse the process, but that should be easy to do once someone helps me to format it to add to the database. Thanks for any help, and I'm sorry if this has been asked a thousand times. Link to comment https://forums.phpfreaks.com/topic/205237-a-bit-of-help-to-a-newbie-date-formating/ Share on other sites More sharing options...
kenrbnsn Posted June 19, 2010 Share Posted June 19, 2010 You have to convert it to a Y-m-d format before inserting it into the database: <?php $date="23/06/1980"; list($d,$m,$y) = explode('/',$date); $new_date = "$y-$m-$d"; echo $new_date; ?> Ken Link to comment https://forums.phpfreaks.com/topic/205237-a-bit-of-help-to-a-newbie-date-formating/#findComment-1074286 Share on other sites More sharing options...
theverychap Posted June 19, 2010 Share Posted June 19, 2010 Now if I echo $new_date, it returns me 1980-23-06 That _is_ a correctly formatted mySQL date... Link to comment https://forums.phpfreaks.com/topic/205237-a-bit-of-help-to-a-newbie-date-formating/#findComment-1074356 Share on other sites More sharing options...
bobby317 Posted June 19, 2010 Share Posted June 19, 2010 This artical helped me a lot with dates maybe you can use it too. http://www.richardlord.net/blog/dates-in-php-and-mysql Link to comment https://forums.phpfreaks.com/topic/205237-a-bit-of-help-to-a-newbie-date-formating/#findComment-1074365 Share on other sites More sharing options...
Tiruak Posted June 19, 2010 Author Share Posted June 19, 2010 You have to convert it to a Y-m-d format before inserting it into the database: <?php $date="23/06/1980"; list($d,$m,$y) = explode('/',$date); $new_date = "$y-$m-$d"; echo $new_date; ?> Ken Thanks alot Ken, This solves the problem and also teaches me a new function that may be very helpful Link to comment https://forums.phpfreaks.com/topic/205237-a-bit-of-help-to-a-newbie-date-formating/#findComment-1074373 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.