Jump to content

Recommended Posts

If you're using the date or datetime field type in mysql, the date is always stored as YYYY-MM-DD. You can change how it look when you display it either when you retrieve it or in PHP by using a combination of the date() and strtotime() functions.

 

Ken

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

If you're using the date or datetime field type in mysql, the date is always stored as YYYY-MM-DD. You can change how it look when you display it either when you retrieve it or in PHP by using a combination of the date() and strtotime() functions.

 

Ken

 

what would happen if u changed the date field in the database to varchar and made sure that the validation checks that the day is valid, is that a bad method to use?

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

There are 2 ways to change the date and time....I prefer to do it in the SELECT statement as it put less load on the webserver and there is less coding that needs to be done.  To do it in the SELECT statement try the following:

 

date_format(FIELDNAME, '%m/%d/%Y - %h:%i:%s %p') as FIELDNAME

 

The other way is you can use the date function built into PHP and do date formatting that way as other people have suggested.

 

Thanks,

 

whit3fir3

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

There are 2 ways to change the date and time....I prefer to do it in the SELECT statement as it put less load on the webserver and there is less coding that needs to be done.  To do it in the SELECT statement try the following:

 

date_format(FIELDNAME, '%m/%d/%Y - %h:%i:%s %p') as FIELDNAME

 

The other way is you can use the date function built into PHP and do date formatting that way as other people have suggested.

 

Thanks,

 

whit3fir3

 

Sorry I only included a small snipit for the SQL code.  The entire select statement would be something like the following:

 

SELECT FIELDNAME, FIELDNAME, FIELDNAME, date_format(FIELDNAME, '%m/%d/%Y - %h:%i:%s %p') as FIELDNAME FROM TABLENAME

 

Any place you see FIELDNAME is the name of a field in the SQL database.  The second in bold is how the Date and Time will be displayed.  You can adjust this to fit your needs.  Right now it will display as MM/DD/YYYY - 00:00:00 am/pm

 

Thanks,

 

whit3fir3

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

what would happen if u changed the date field in the database to varchar and made sure that the validation checks that the day is valid' date=' is that a bad method to use?[/quote']

 

If you store date as a string in "dd/mm/yyyy" or "mm/dd/yyyy", or any way but yyyy-mm-dd (or yyyymmdd) then it is totally useless in a database. You cannot compare dates, sort by date, select ranges of dates, use datetime functions....

 

Got the idea?

 

Use DATETIME or DATE type using the ISO format YYYY-MM-DD.

 

Alternatively you could use INT and store as a Unix timestamp but this has 2 disadvantages

 

  • If you use a tool like QueryBrowser or PHPMyAdimin to view the data they are totally unrecognisable
     
  • Most MySQL datetime functions expect a DATE/DATETIME so you are forever having to convert

My 0.02

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

what would happen if u changed the date field in the database to varchar and made sure that the validation checks that the day is valid' date=' is that a bad method to use?[/quote']

 

If you store date as a string in "dd/mm/yyyy" or "mm/dd/yyyy", or any way but yyyy-mm-dd (or yyyymmdd) then it is totally useless in a database. You cannot compare dates, sort by date, select ranges of dates, use datetime functions....

 

Got the idea?

 

Use DATETIME or DATE type using the ISO format YYYY-MM-DD.

 

Alternatively you could use INT and store as a Unix timestamp but this has 2 disadvantages

 

  • If you use a tool like QueryBrowser or PHPMyAdimin to view the data they are totally unrecognisable
     
  • Most MySQL datetime functions expect a DATE/DATETIME so you are forever having to convert

My 0.02

 

 

Thanks for the reply, my code for the date validation is below, can i easily change the code to validate the date as 0000/00/00?

 

//DOB check) 

if (empty($dob)) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a dob
$dob_message = '*Please enter your date of birth*';
}

//Check the format and explode into $parts
  elseif (!ereg("^([0-9]{2})/([0-9]{2})/([0-9]{4})$", 
          $dob, $parts)){
$error_stat = 1; 	

//Set the message to tell the user the date is invalid
$dob_message = '*Invalid dob, must be DD/MM/YYYY format*';
}
    
  elseif (!checkdate($parts[2],$parts[1],$parts[3]))
  {
  $error_stat = 1; 
  
  //Set the message to tell the date is invalid for the month entered
$dob_message = '*Invalid dob, month must be between 1-12*';
}

elseif (intval($parts[3]) < 1948 || 
          intval($parts[3]) > intval(date("Y")))
  {
    
    $error_stat = 1; 

   //Set the message to tell the user the date is invalid for the year entered
$dob_message = '*Invalid dob, year must 1948 onwards*';
  }

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

Could you still sort by the FIELDNAME column?

 

There are 2 ways to change the date and time....I prefer to do it in the SELECT statement as it put less load on the webserver and there is less coding that needs to be done.  To do it in the SELECT statement try the following:

 

date_format(FIELDNAME, '%m/%d/%Y - %h:%i:%s %p') as FIELDNAME

 

The other way is you can use the date function built into PHP and do date formatting that way as other people have suggested.

 

Thanks,

 

whit3fir3

 

Sorry I only included a small snipit for the SQL code.  The entire select statement would be something like the following:

 

SELECT FIELDNAME, FIELDNAME, FIELDNAME, date_format(FIELDNAME, '%m/%d/%Y - %h:%i:%s %p') as FIELDNAME FROM TABLENAME

 

Any place you see FIELDNAME is the name of a field in the SQL database.  The second in bold is how the Date and Time will be displayed.  You can adjust this to fit your needs.  Right now it will display as MM/DD/YYYY - 00:00:00 am/pm

 

Thanks,

 

whit3fir3

Link to comment
https://forums.phpfreaks.com/topic/89763-mysql-date-format/#findComment-460251
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.