techker Posted January 15, 2013 Share Posted January 15, 2013 Hey guys i insert my date as:01/14/2013 so m-d-y how can i make it so that the mysql recognizes my dates: i got this but it doesnt work.. $today = date('m-d-Y'); $d1 = 'SELECT * FROM '. $row5['Fiscale_Sortie'] .' WHERE DATE(Date_Rempli) = "$today"'; $result8 = mysql_query($d1) or die(mysql_error()); $total_rows8 = mysql_fetch_row($result8); Quote Link to comment https://forums.phpfreaks.com/topic/273165-date-format/ Share on other sites More sharing options...
Barand Posted January 15, 2013 Share Posted January 15, 2013 What format is Date_Rempli Quote Link to comment https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1405739 Share on other sites More sharing options...
techker Posted January 15, 2013 Author Share Posted January 15, 2013 The field were i store the date Quote Link to comment https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1405751 Share on other sites More sharing options...
requinix Posted January 15, 2013 Share Posted January 15, 2013 But what format is it? What type of column? DATE? DATETIME? VARCHAR? MySQL uses Y-M-D so try with your $today formatted that way. Quote Link to comment https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1405763 Share on other sites More sharing options...
techker Posted January 15, 2013 Author Share Posted January 15, 2013 ya but my error was to take over somebody else's work and they insert into varchar M-D-Y and there's like already over 400 entries... Quote Link to comment https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1405817 Share on other sites More sharing options...
Barand Posted January 15, 2013 Share Posted January 15, 2013 You cannot use DATE(), or most other mysql datetime functions on your field as it is. If it is a string in m-d-y format then = '01-15-2012', for example should work. Equal to is about the only comparison that will. You can use STR_TO_DATE to format as DATE type Quote Link to comment https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1405848 Share on other sites More sharing options...
TRI0N Posted January 15, 2013 Share Posted January 15, 2013 (edited) You will need to explode your m-d-Y to Y-m-d before inserting or updating into your database. $my_date_format = '1-15-2013' ; $date_fix = explode('-', $my_date_format) ; $new_month = $date_fix[0] ; $new_day = $date_fix[1] ; $new_year = $date_fix[2] ; $new_date_to_store = $new_year.'-'.$new_month.'-'.$new_day ; $new_date_to_store is the date format you will want to store into your database. Then you will need to do the exact same thing to reverse the date upon SELECT to parse it to the format you want to see when viewed on a page. $db_date_format = $row['date'] ; $date_fix = explode('-', $db_date_format) ; $new_year = $date_fix[0] ; $new_month = $date_fix[1] ; $new_day = $date_fix[2] ; $new_date_to_display = $new_month.'-'.$new_day.'-'.$new_year ; echo $new_date_to_display ; Edited January 15, 2013 by TRI0N Quote Link to comment https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1405918 Share on other sites More sharing options...
TRI0N Posted January 15, 2013 Share Posted January 15, 2013 The only problem that storing dates in VARCHAR is that PHP Date functions have more flexibilities when you wish to short or calculate dates in the correct date format. If you do this those functions will not work like they should if the date is not in its standard format. Quote Link to comment https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1405922 Share on other sites More sharing options...
TRI0N Posted January 15, 2013 Share Posted January 15, 2013 I just noticed your original date format is 1/15/2013 The correct code to convert: $my_date_format = '1/15/2013' ; $date_fix = explode('/', $my_date_format) ; $new_month = $date_fix[0] ; $new_day = $date_fix[1] ; $new_year = $date_fix[2] ; $new_date_to_store = $new_year.'-'.$new_month.'-'.$new_day ; The correct code to display: $db_date_format = $row['date'] ; $date_fix = explode('-', $db_date_format) ; $new_year = $date_fix[0] ; $new_month = $date_fix[1] ; $new_day = $date_fix[2] ; $new_date_to_display = $new_month.'/'.$new_day.'/'.$new_year ; echo $new_date_to_display ; That will do the trick! Quote Link to comment https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1405923 Share on other sites More sharing options...
Barand Posted January 15, 2013 Share Posted January 15, 2013 You could update your dates with a single line of mysql UPDATE mytable SET datefield = STR_TO_DATE(datefield, '%e-%m-%Y')[code] then change the type to DATE Quote Link to comment https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1405995 Share on other sites More sharing options...
Christian F. Posted January 16, 2013 Share Posted January 16, 2013 Also, to properly convert the MySQL timestamp to another format (for displaying) you can either use the built-in functions for MySQL, or the DateTime class in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1406046 Share on other sites More sharing options...
techker Posted January 17, 2013 Author Share Posted January 17, 2013 You could update your dates with a single line of mysql UPDATE mytable SET datefield = STR_TO_DATE(datefield, '%e-%m-%Y')[code] then change the type to DATE thx for the help guys.this will take the date a convert it? Quote Link to comment https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1406513 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.