Jump to content

[SOLVED] Best way to store a date?


dmccabe

Recommended Posts

I need to store a date in a mySQL db.

 

The date will be entered by a user in the format: dd/mm/yy

 

I need to store it so it can be displayed as dd/mm/yy, but I also need to be able to perform calculations on it to find out the number of days since that date.

 

Could anyone suggest to me the best way to store it for these options?

Link to comment
Share on other sites

With a time stamp i don't need to play with strtotime(),

 

days >> 7 * hours >>  24 * min >>  60 *  >> sec 60 very easy better in my opinion

 

I find it a lot easier, To get the time stamp and manipulate the time stamp for any purpose.

 

example.

<?php

//from $date=time(); in the database as a int.

$date="1230553405";

$d=date("D/M/Y",($date));

echo $d;

?>

Link to comment
Share on other sites

Sorry to interupt the discussion, but just doing a little testing with MChl suggestion as follows:

 

$mdate = date("d-m-Y",strtotime("28/09/2008"));
$yourDate = date("d/m/y",strtotime($mdate));
echo "Date $yourDate <br />";

 

this just echo's out 01/01/70   no matter what I put in place of 28/09/2008 ?

 

Am I missing something here.  Obviously I am not writing it in to a db yet, is that where the magic happens?

Link to comment
Share on other sites

add date to database

 

<?php
$date=$_POST['date'];
$date=date("d/m/y");
?>

 

try this,

 

this is examples to manipulate date not insert to database, get from database.

<?php

$date=date("d/m/y",mktime(0,0,0,"28","09","2008"));

$yourDate = date("d/m/y",strtotime($date));

echo "Date $yourDate <br />";

?>

Link to comment
Share on other sites

Please, do not store a DATE as an INT in the database. They made a DATE field for a reason. As Mchl pointed out there are so many MySQL functions to handle dates that make it very easy.

 

I suggest storing as DATETIME and using NOW() on inserts (unless you don't want the current time):

<?php
$sql = "INSERT INTO table_name (field_name, date_added) VALUES (123, NOW())"; 
mysql_query($sql, $conn) or die(mysql_error().$sql);
?>

Link to comment
Share on other sites

Sorry, I did not check my code. It seems that strtotime cant parse dd\mm\yyyy date

 

Go with what redarrow proposes or

 

<?php
$mdate = date("d-m-Y",strtotime(str_replace("/","-","28/09/2008")));
$yourDate = date("d/m/y",strtotime($mdate));
echo "Date $yourDate <br />";
?>

 

redarrow: all I'm saying is that storing dates as timestamp is at most equally good as storing them as DATEs. It depends a lot, if you will only select dates from database, or if you're also going to use them in lots of queries.

 

Personally I will go with DATE.

Link to comment
Share on other sites

Thanks again all, no doubt there will be more questions, but just one quick query for you Mchl

 

Is there a reason you choose the format d-m-Y (with capital Y) and put the date in as 2008 not 08 ?

 

MySQL will like you more for that :)

 

Actually, I just don't remember if MySQL will consume d-m-y date... and since I know it will go fine with d-m-Y... I go with that ;)

Link to comment
Share on other sites

ah that now makes sense :)

 

hoorah!!!

 

Date's are now being stored and displayed in the correct format :)

 

Ok so next Question.

 

Lets say I wanted to select all items where the date was more than 10 days ago, how would I structure the SQL query for that?

Link to comment
Share on other sites

For redarrow:

 

If  date was stored as timestamp, we would do

 

SELECT * FROM table WHERE DATEDIFF(NOW(),FROM_UNIXTIME(timestamp)) > 10

 

so not much more complicated. But imagine you have more complex condition involving several date fields... Query wuold get less understandable really quick.

I imagine it would be a bit slower as well, but I have nothing to back it up.

 

So that's why I recommend DATE, TIME, DATETIME and TIMESTAMP (note TIMESTAMP is not INT timestamp). ;)

Link to comment
Share on other sites

 

 

strtotime() and date() are some of the slower php functions. Using them for simple rearranging of date parts is a waste of time. Just explode the original format and concatenate back in the correct format or more simply use the mysql STR_TO_DATE() function in your query.

 

Dates/times stored as Unix Timestamps must under go a slow conversion (FROM_UNIXTIME(), strtotime() and date()) for most purposes and that conversion is subject to errors if the time zone setting is ever changed from when the Unix Timestamp was created or if your DST database is not up to date with current DST start/end dates. However, a DATE data type - 2008-12-28 will always be that value and to format it as any other value using the mysql DATE_FORMAT() is quick because changing the format does not require multiple slow conversions to/from a Unix Timestamp.

Link to comment
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.