Current location: Hot Scripts Forums » Other Discussions » Database » Storing dates in MySQL


Storing dates in MySQL

Reply
  #1 (permalink)  
Old 03-27-08, 09:51 PM
cnapsys cnapsys is offline
Newbie Coder
 
Join Date: Feb 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Storing dates in MySQL

Hi all,
I have a few questions about storing php timestamps in mysql tables.
What is the best mysql type field for storing a unix timestamp?
What are the advantages of using varchar type or int type? Any other type i should consider?
My problem is that i will be having a lot of scripts for comparing the date fields and I just wanna make sure I do my best from the beginning.
There will be a lot of entries in the db and i'm looking for the most efficient memory and processing usage as well as fastest time for queries.

Bottom line:
What is the best type? Why?
Reply With Quote
  #2 (permalink)  
Old 03-28-08, 12:37 AM
DAL's Avatar
DAL DAL is offline
Code Master
 
Join Date: Jun 2003
Location: North East England/UK
Posts: 874
Thanks: 0
Thanked 0 Times in 0 Posts
Best type:I would imagine the date field would be the best for dates.
Why:Because it was made for dates.

VarChar is limited to 255 characters which as you know is better for access as the memory allocation for this field is low.

Int is a general value integer. consider tinyint where you can get away with it.

I use
varchar for titles and names,
int for index record numbers and large stock qantities,
float for currency and numbers with sigfig
tinyint for small quantities and low count processing,
text for text which needs no formatting,
longtext for my unlimited entry fields (usually HTML Scripts and such)
Date of YYYY-MM-DD HH:MM:SS:MS for this. I think you can preformat this field but Ive never needed to, I just switch it all around using a PHP function I wrote years ago.

Thats pretty much me. Im not too knowledgable on DB's and PHPMyAdmin has buttons I may someday dream of pressing and understanding.


Hope this helps
Dal.
__________________
"once upon a midnight dreary, while i pron surfed, weak and weary, over many a strange and spurious site of 'hot xxx galore'. While i clicked my fav'rite bookmark, suddenly there came a warning, and my heart was filled with mourning, mourning for my dear amour," 'Tis not possible!", i muttered, "give me back my free hardcore!" quoth the server, 404."
Reply With Quote
  #3 (permalink)  
Old 03-28-08, 02:45 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
For a date, use type DATE,
for a time, use type TIME,
for a date and time, use type DATETIME

Really is that simple lol
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #4 (permalink)  
Old 03-28-08, 03:47 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Quote:
Originally Posted by DAL View Post
VarChar is limited to 255 characters which as you know is better for access as the memory allocation for this field is low.
But it is much slower to access as your mysql server will have to determine the length of the field (of each record in your table) everytime it wants to read in the value of it. A CHAR might use a little more memory (it all depends on what you're going to store in it), but is a lot faster as it has a fixed with.
This means that the filepointer can simply jump to the next field, whereas, with a VARCHAR, it first needs to read the first byte of the field to determine the length of the total field, and then jump to the next field

Quote:
Originally Posted by DAL View Post
Int is a general value integer. consider tinyint where you can get away with it.
I doubt the fact that cnapsys would get away with a TINYINT (1 byte) as the values only range from -127 to 127 if it's a signed INT, and 0 to 255 if it's not. If I recall correctly, a unixtime has a value with at least 9 digits.
He should at least use an INT (4 bytes) or even a BIGINT (8 bytes)


As for the DATE, TIMESTAMP and TIME functions, have a look at the DATETIME, DATE and TIMESTAMP Types in the MySQL manual



Oh yes, I'm moving this to the Database forum, as this is more of database related problem
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks


Last edited by UnrealEd; 03-28-08 at 03:49 AM.
Reply With Quote
  #5 (permalink)  
Old 03-28-08, 04:51 AM
cnapsys cnapsys is offline
Newbie Coder
 
Join Date: Feb 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
I really appreciate your help guys.
However, my question was on storing a unix timestamp which is the amount of seconds from January 1, 1970.
It will not be in the format of YYYY-MM-DD HH:MM:SS:MS... that is a DATETIME type or TIMESTAMP, but I will basically be working with integers.
E.g. the following date 2008-03-27 21:08:00 has the following Unix timestamp: 1206670080
The reason for storing it as a unix timestamp is for easier comparison within the scripts.
I'm just trying to wheigh my options and see why some people choose to store the value in a varchar field while others prefer int.

Please shed some light.
Thank you
Reply With Quote
  #6 (permalink)  
Old 03-28-08, 05:06 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
It's very easy to convert a DATE, TIME or DATETIME field to a unix timestamp, using the UNIX_TIMESTAMP function. It still is best to save it as either a DATE, TIME or DATETIME field. simply convert the unixtime to a DATETIME object using a very simple php workaround:
PHP Code:

mysql_query (sprintf ("INSERT INTO table (`date`) VALUES ('%s')"date ("YmdHis"time ()))); 

If you still want to store the unixtime itself, you'll have to use an INT field or a CHAR/VARCHAR field
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #7 (permalink)  
Old 03-28-08, 05:58 AM
cnapsys cnapsys is offline
Newbie Coder
 
Join Date: Feb 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Hey Ed, and thx again for your fast reply...

here's my situation:
Lets take the following 3 dates

PHP Code:

$a=date("2008-03-28 18:00:00"); //today's date... random time

$b=date("2008-03-31 18:00:00"); //random date and time within the week's interval
$c=date("2008-04-04 18:00:00"); //end date... a week from today 
the following:
PHP Code:

$atime=strtotime($a);

$btime=strtotime($b);
$ctime=strtotime($c); 
returns:
1206745200
1207004400
1207350000

You can clearly see why I wanna choose storing these values into MySQL

Lets say we have a quick small search engine that it is supposed to display events between the following dates: 2008-03-28 and 2008-04-04.
By storing the unix timestamp into mysql its a lot easier to build the queries by conditioning the time interval the results should fall within.

If I chose to store the values in the YYYY-MM-DD HH:MM:SS:MS format I'd have to go thru a lot of explosions and arrays to compare each segment of the date to each other.
Reply With Quote
  #8 (permalink)  
Old 03-28-08, 06:24 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by cnapsys View Post
If I chose to store the values in the YYYY-MM-DD HH:MM:SS:MS format I'd have to go thru a lot of explosions and arrays to compare each segment of the date to each other
Not true. MySQL has a number of functions built in to prevent these kind of problems. See here for a list of them
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #9 (permalink)  
Old 03-28-08, 07:28 AM
cnapsys cnapsys is offline
Newbie Coder
 
Join Date: Feb 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
wow... thanks for the resource Jay... never checked that before...
I'll give it a quick look and see if I can implement that into my system...

Gotta love the open source communities... you learn something new every day!
Reply With Quote
  #10 (permalink)  
Old 03-28-08, 07:32 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Yup indeedy
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
Comparing 2 dates on MySQL entries m_abdelfattah Database 2 02-05-07 04:50 PM
MySQL Dates DAL PHP 5 04-22-06 07:00 AM
ASP< MySQL and Dates! gillweb ASP 1 04-05-06 05:23 PM
Storing PHP Code in MySQL Cheney PHP 1 08-07-05 09:25 PM


All times are GMT -5. The time now is 05:02 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.