Jump to content

[SOLVED] Separating a predefined date that is stored in a database?


Recommended Posts

hi everyone

 

I'm trying to find out if/how I can separate a date that I would have already stored in a database into  $month, $day and $year  since I'll be using selection boxes for inputting the date and obviously when it comes to editing info on say a movie I would need the right release date showing

I've tried looking round for how to do and tried a few things but no luck, can anyone help me or would it actually better if I just stored the date in the database a separate field (month, day, year).

 

Just in case it's useful here is a link to what I made learning how to make the date selection boxes and the code below

 

 

http://bulletsnoctane.co.uk/date_select.php

 

<?php
$d2 = $_POST['d2'];
$m2 = $_POST['m2'];
$y2 = $_POST['y2'];
if($y2 == "")
$y2 = date("Y");
?>

<form name=xc action="" method=POST>


<?php
/* ----------------------- Day Select Box  ----------------------------

*/

echo "<select name=d2>";
for($value=1; $value<32; $value++)
{
if($d2 == $value)
	echo "<option value=$value selected>$value</option>";
else
	echo "<option value=$value>$value</option>";
}

echo "</select>";


/* ----------------------- Month Select Box  ----------------------------

The Month Select Box So the Value is the numeric value but but what the user selects is the actual text value of  a month like "May" */

/* create array so we can name months */ 
$monthName = array(1=> "January", "February", "March", 
"April", "May", "June", "July", "August", 
"September", "October", "November", "December");

echo "<select name=m2>";

for($value = 1; $value <= 12; $value++) 
{ 
echo "<option value=\""; 
echo intval($value); 
echo "\""; 
if($m2 == $value) 
{ 
echo " SELECTED"; 
} 
echo ">" . $monthName[$value] . "</option>"; 
}

echo "</select>\n";


/* ----------------------- Year Select Box  ----------------------------

*/

echo "<select name=y2>";

$startYear = date( "Y"); 
for($value = $startYear - 75; $value <= $startYear+5;$value++) 
{ 
echo "<option value=\"$value\""; 
if($y2==$value) 
{ 
echo " SELECTED"; 
} 
echo ">" . $value . "</option>"; 
} 

echo "</select>\n";

?>
<input type=submit value="view stats">
</form>

<?php



			$date1 = date("Y-m-d",mktime(0, 0, 0, $m2, $d2, $y2));
			include "action.php";



?>

I'm assuming that you're storing the date in the database as a datestamp rather than a VARCHAR2.

 

What exactly are you trying to do?

I'm not certain if you're getting the user to enter a date and then planning on searching for all films due for release on that date, or what?

its currently getting stored as a date data type

 

Im making a movie site that as well as news and review im trying to include more detail info about the actual movies and actors like what you would find at imdb.com

 

so im storing information about the movie(name, release date, summary, type etc) and then the users would enter/edit the data, so when it comes to the user editing the data i want the selection boxes to show the correct release date if one has already been entered

First question.

 

Where are you pulling the dates out of the database in that script?

 

 

As far as storing dates in a database, store them as the actual date and don't do 3 separate fields. It is easier to modify a date as a timestamp than as 3 separate fields. As mentioned above you would use either the explode and strtotime functions to separate your date into the sections you want, day/month/year and then set it much like you have done with the $_POST, except with the data from the database.

 

im not pulling any data from the database with that code premiso if i miss led you.  code was just me trying to show people how i had the selection box and the date split up.

 

Thank you Mchl for the exmple that is exactly what i was after

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.