Jump to content

Recommended Posts

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);

Link to comment
https://forums.phpfreaks.com/topic/273165-date-format/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1405848
Share on other sites

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 by TRI0N
Link to comment
https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1405918
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1405922
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/273165-date-format/#findComment-1405923
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.