Jump to content

MYSQL Date


OriginalBoy

Recommended Posts

Hey,

 

I have set up a mysql database with a date feature. I need this date feature to work so when it is submitted it will show the date it was submitted. Everything is done apart from this so I am very keen to get it finished. I made the table in phpmyadmin and now I do not know whats next. If its to complicated to explain in a post feel free to add me on msn steve@whitepixeldesign.com.

 

Thanks

Link to comment
Share on other sites

Basically when ever the person has submitted whatever they need to submit, I suspect that you already have something that tells PHP, "Okay, the whatever is submitted now we need to execute this", if not, then post what it is you are trying to do and we would help you with that. As for the date in mysql, the mysql date format is as follows "year-month-day" i'll give you a little snippet below and all it does is show you how you would insert the date into mysql, and if you would like to show the date to users as "month-day-year" I will also include a function for that. Here we go,

 

<?php
$date = date('Y-m-d');

//I assume you know where to put the name of your table, and the row for the date 
$query = "INSERT INTO your_table (row_name) VALUES ('$date')";
mysql_query($query);
?>

 

As for fetching, and displaying the date here's what I do.

 

<?php
//function to convert the date inputted 
//to the following format
//month-day-year
function convert_date($date)
{
$ex = explode("-", $date);
$dates = "{$ex[1]}-{$ex[2]}-{$ex[0]}";
return $dates;	
}

//echo the date
$s_date = convert_date($your_date);

echo $s_date;
?>

 

Hope I explained fairly well, I am after all only 16 and not very good with explaining haha, if you need any help just let me know. :)

Link to comment
Share on other sites

So if you have in your table 3 rows...ID, (id) the date posted (p_date) and the content (content)

 

to upload,

mysql_query("INSERT INTO example (id, p_date, content) VALUES(null, NOW(), 'whatever' ) ") or die(mysql_error());   

 

to display it row by row

$result=mysql_query("SELECT * FROM example");

while ($row = mysql_fetch_array($result)){
echo 'ID - '.$row['id'].'<br>';
echo 'Date - '.$row['p_date'].'<br>';
echo 'Content - '.$row['content'].'<br>';
}

 

try that >_>

 

(adapt it of course)

Link to comment
Share on other sites

So if you have in your table 3 rows...ID, (id) the date posted (p_date) and the content (content)

 

to upload,

mysql_query("INSERT INTO example (id, p_date, content) VALUES(null, NOW(), 'whatever' ) ") or die(mysql_error());   

 

to display it row by row

$result=mysql_query("SELECT * FROM example");

while ($row = mysql_fetch_array($result)){
echo 'ID - '.$row['id'].'<br>';
echo 'Date - '.$row['p_date'].'<br>';
echo 'Content - '.$row['content'].'<br>';
}

 

try that >_>

 

(adapt it of course)

 

Do i need to make a new file or add it to an existing one?

Link to comment
Share on other sites

So if you have in your table 3 rows...ID, (id) the date posted (p_date) and the content (content)

 

to upload,

mysql_query("INSERT INTO example (id, p_date, content) VALUES(null, NOW(), 'whatever' ) ") or die(mysql_error());   

 

to display it row by row

$result=mysql_query("SELECT * FROM example");

while ($row = mysql_fetch_array($result)){
echo 'ID - '.$row['id'].'<br>';
echo 'Date - '.$row['p_date'].'<br>';
echo 'Content - '.$row['content'].'<br>';
}

 

try that >_>

 

(adapt it of course)

 

Do i need to make a new file or add it to an existing one?

 

That's just how you fetch the information include it in your current file and use variables to store the information and display it only whenever you need it. If that makes since?

Link to comment
Share on other sites

So if you have in your table 3 rows...ID, (id) the date posted (p_date) and the content (content)

 

to upload,

mysql_query("INSERT INTO example (id, p_date, content) VALUES(null, NOW(), 'whatever' ) ") or die(mysql_error());   

 

to display it row by row

$result=mysql_query("SELECT * FROM example");

while ($row = mysql_fetch_array($result)){
echo 'ID - '.$row['id'].'<br>';
echo 'Date - '.$row['p_date'].'<br>';
echo 'Content - '.$row['content'].'<br>';
}

 

try that >_>

 

(adapt it of course)

 

Do i need to make a new file or add it to an existing one?

 

That's just how you fetch the information include it in your current file and use variables to store the information and display it only whenever you need it. If that makes since?

 

The point im trying to get to is where do i add the code i made the table in PHPmyadming now I keep getting a load of code but have no idea what to do with it and where to add it.

Link to comment
Share on other sites

Wherever you want to display the results of your table, in a page, just make a new file with a .php ending, in between these tags:

 

<?php

?>

 

 

and then write the code i gave you, modified to your table. Post the column headings and we'll help you.

Link to comment
Share on other sites

 

to upload,

mysql_query("INSERT INTO example (id, p_date, content) VALUES(null, NOW(), 'whatever' ) ") or die(mysql_error());   

 

What would go in the 'whatever' ?

 

the 'whatever' area is for instance if you wanted a user to post stories the story variable would go in place of 'whatever'

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.