Jump to content

Recommended Posts

Hey Guys,

 

I have an input field that takes in the date in the format of "07/22/2009". In the database where I want to store this, the field is datetime. However, when I try to save this to the DB it stores it as "0000-00-00 00:00:00".

 

Just wondering, do I have to convert this to a different timestamp or something? Just not 100% sure. Then if I need to read it from the DB agai.. do I ned to re-convert it. How id this generally handled?

Link to comment
https://forums.phpfreaks.com/topic/165767-date-time-question/
Share on other sites

The conversion to/from a Unix Timestamp is both somewhat slow and error prone (depends on the time zone setting at the time of each conversion and on how up to date your DST database is.) On the other hand, a DATE or DATETIME entered into a database will always have the correct value it was entered as and can be formatted any way you want. Formatting a DATE or DATETIME is about 8-10 times faster (use the mysql DATE_FORMAT() function in your SELECT query) than the conversion a Unix Timestamp needs to get it into a usable format.

 

I have an input field that takes in the date in the format of "07/22/2009"
Getting a human to correctly enter a date in the correct format every time is not going to happen. It is actually better if you provide three drop-down select menus or a javascript date-picker so that they can only pick dates it the correct format.

 

the field is datetime
If you are only storing the date, just use a DATE data type, not a DATETIME.

 

The format for a DATE field is YYYY-MM-DD. Attempting to put a value into a DATE field that is not in the correct format results in the minimum value defined for that field. To get your "07/22/2009" format into a YYYY-MM-DD format, just explode it on the '/' and arrange the parts in the format you need them -

 

$yourdate = "07/22/2009";
list($month,$day,$year) = explode('/',$yourdate);
if(checkdate($month,$day,$year)){
echo "Valid date<br />";
$date = "$year-$month-$day";
echo $date;
} else {
echo "Invalid date<br />";
}

 

If you have already validated the incoming date, a second method to get the mm/dd/yyyy format into a YYYY-MM-DD is to use the mysql STR_TO_DATE() function directly in your INSERT query.

Link to comment
https://forums.phpfreaks.com/topic/165767-date-time-question/#findComment-874576
Share on other sites

  • 2 weeks later...

The conversion to/from a Unix Timestamp is both somewhat slow and error prone (depends on the time zone setting at the time of each conversion and on how up to date your DST database is.) On the other hand, a DATE or DATETIME entered into a database will always have the correct value it was entered as and can be formatted any way you want. Formatting a DATE or DATETIME is about 8-10 times faster (use the mysql DATE_FORMAT() function in your SELECT query) than the conversion a Unix Timestamp needs to get it into a usable format.

 

Didn't knew that, thank you ;)

Link to comment
https://forums.phpfreaks.com/topic/165767-date-time-question/#findComment-880363
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.