Jump to content

Storing dates


Mutley

Recommended Posts

Which is the best method to select and store Dates.

 

I have 3 dropdowns, for Day, Month and Year. So do I save these to 3 separate fields or use 1 field and convert it into a "date" style format.

 

What I'm wanting to down the line, is compare the date entered to the current date (using a simple date('d-M-Y') style thing and I imagine using 3 separate fields wouldn't work as how would I convert it to a date for use in comparing to the PHP date function?

 

Thanks,

Nick.

Link to comment
Share on other sites

Store it as a date in the MySQL table.  That way you can do easy comparisons in your SQL statements in the future:

$query = "SELECT * FROM table WHERE the_date < '" . date('Y-m-d') . "'";

 

Just join the three values from your form into a string when you insert.

Link to comment
Share on other sites

There is probably a better way to do this, but this should work:

$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];

$timestamp = strtotime("$year-$month-$day");

$datetime = date('Y-m-d H:i:s', $timestamp);

$query = "INSERT INTO table SET datetime_col = '$datetime'";

Link to comment
Share on other sites

  • 2 weeks later...

Ah, of course.

 

So it's just a simple insertion of:

$date = '$year-$month-$day';

 

Then can you use date() to select them? If I wanted to select just the month would I just do date('m'); or doest that only work with stamps?

 

Thanks a lot,

Nick.

Link to comment
Share on other sites

So it's just a simple insertion of:

$date = '$year-$month-$day';

 

Yep - though you'd obviously want to sanitize that data.

 

Then can you use date() to select them? If I wanted to select just the month would I just do date('m'); or doest that only work with stamps?

 

What do you mean? Are you trying to format the data when you grab it from the database? If so, then use the MySQL DATE_FORMAT() function : http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

 

 

Link to comment
Share on other sites

Thanks, I've nearly got it! I filter the dates like this:

 

<?php
$sql  = "SELECT date, event FROM calendar WHERE YEAR(date) >= $thisyear AND MONTH(date) >= $thismonth ORDER BY date DESC LIMIT 10";
$result = mysql_query($sql);
	while(list($date, $event) = mysql_fetch_row($result)) {
?>

 

Which works great, just how do I with PHP select just the 'day' from the date SQL field?

 

If I do

date('j',$date);

 

It doesn't work. Which I guess is because it isn't a time stamp. I can set new DATE_FORMAT in the query but it's a lot of extra code to do it for each day/month/year.

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.