Jump to content

Archived

This topic is now archived and is closed to further replies.

sharkyJay

datetime or date and time?

Recommended Posts

From a practical point of view is it better to store the date and time in one field or have two?

Would have time as separate make it easier to do analysis?

Share this post


Link to post
Share on other sites
It's easier to have a field with the date and time. You can always use date formating functions and get the time, or the date, or both.

Share this post


Link to post
Share on other sites
It is best to store the date and time as a unix timestamp in one field. Then when you get the timestamp out of the database you can use the date function to format the time stamp:
[code]date("d-m-y", $row['timestamp']);[/code]
Also you can easily compare dates too with timestamps.

Share this post


Link to post
Share on other sites
A lot of people like UNIX timestamps, but be careful with them because they are messy when you have dates prior to January 1 1970.

It depends on what you are going to store, but quoting Barand:

[quote]@poirot

I was born in 1949 - try storing my date of birth in a UNIX timestamp.[/quote]

This was when I realized UNIX timestamps were not magical solutions.

Share this post


Link to post
Share on other sites
Umm, thats a good point there poirot I always forget about unix timestamp will only work with dates prior to Jan 1 1970! But unix timestamp is okay for doings things such as logging when someone posts a message or logs in etc but not with DoB's

Share this post


Link to post
Share on other sites
Be careful when trying to select records by date if you have both data and time elements the column

[code]2006-06-12 05:30:00
2006-06-12 09:30:00
2006-06-12 15:30:00

$date = date ('Y-m-d');

SELECT * FROM mytable WHERE datecol = '$date'[/code]

gives no rows.

Share this post


Link to post
Share on other sites
So I can use this, right?

[code]$date = date ('Y-m-d');

SELECT *, DATE_FORMAT(date_col, '%Y-%m-%d') AS c_date FROM mytable WHERE c_date = '$date'[/code]

Share this post


Link to post
Share on other sites
Or use PHP, which I often do, contrary to your belief ;-), and

[code]$d1 = date ('Y-m-d 00:00:00');
$d2 = date ('Y-m-d 23:59:59');

SELECT * FROM mytable WHERE datecol BETWEEN '$d1' AND '$d2'[/code]

Share this post


Link to post
Share on other sites

×

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.