JuanKiller69 Posted June 28, 2011 Share Posted June 28, 2011 Hello guys! this is my first post, hope I can have some help soon... In a CSV file I got these fields with dates that look like this: ... , ... , ... , ... , ... ,09/05/2011,15/05/2011, I'm gonna put them in a MySQL table, where the fields are DATE TYPE, BUT if I put them as a normal string, like this: (Let's asume that I have all that funny CSV data inside an array) INSERT INTO thetable VALUES("...", "...", "$values_array[thekey]", "$values_array[$theotherkey]", ..., ...); it will show me 00-00-0000!!! :S So, I'm using this functuin strtotime to fix it: ($record is the name of the actual array, dont worry about it) $date1 = $record[8]; $date1 = date("Y-m-d", strtotime($date1)); $date2 = $record[9]; $date2 = date("Y-m-d", strtotime($date2)); Now, I use this query: $query = "INSERT INTO thetable VALUES ('', '', '', '$date1', '$date2', '', '', '', '', '', '', '')"; and it shows me the date1 correctly, like: 2011-09-05, BUT The date2 is always wrong! even when the string inserted ($record[9]) is 2011/15/05, the date it tries to insert is "1969-12-31" <---- !!! What's wrong, why does it work with the first date and does not with the second one? Thanks for your help beforehand! Link to comment https://forums.phpfreaks.com/topic/240592-need-help-with-function-date-and-strtotime/ Share on other sites More sharing options...
WebStyles Posted June 28, 2011 Share Posted June 28, 2011 even your first date is incorrect. strtotime is looking for the month in the wrong field, and 15 is an invalid month so you get an incorrect date. you first date is 9th May 2011, but you're inserting 5th September 2011. (i.e. day and month are switched) try splitting it manually: $date1 = explode("/",$record[8]); $realdate1 = $date1[2].'-'.$date1[1].'-'.$date1[0]; $date2 = explode("/",$record[9]); $realdate2 = $date2[2].'-'.$date2[1].'-'.$date2[0]; Link to comment https://forums.phpfreaks.com/topic/240592-need-help-with-function-date-and-strtotime/#findComment-1235803 Share on other sites More sharing options...
cssfreakie Posted June 28, 2011 Share Posted June 28, 2011 or instead of explode do: $date = '15/05/2011'; $date = str_replace('/','-', $date); // because 15-05-2011 is valid also have a look here: http://www.php.net/manual/en/datetime.formats.date.php Link to comment https://forums.phpfreaks.com/topic/240592-need-help-with-function-date-and-strtotime/#findComment-1235805 Share on other sites More sharing options...
salathe Posted June 28, 2011 Share Posted June 28, 2011 If you're using PHP 5.3, then use DateTime::createFromFormat() (if you're not, why not?!). $date = '15/05/2011'; $datetime = DateTime::createFromFormat('d/m/Y', $date); $mysql_date = $datetime->format('Y-m-d'); If you want to stick with strtotime() then formats that it will accept are detailed under Date Formats. Link to comment https://forums.phpfreaks.com/topic/240592-need-help-with-function-date-and-strtotime/#findComment-1235831 Share on other sites More sharing options...
JuanKiller69 Posted June 28, 2011 Author Share Posted June 28, 2011 THANK YOU FOR YOUR REPLY!!! I've solved the problem using the function explode() mentioned above, it formats the date the way I need it. Thanks for sharing your ideas, gonna be back soon! Link to comment https://forums.phpfreaks.com/topic/240592-need-help-with-function-date-and-strtotime/#findComment-1235901 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.