Jump to content

Inserting Date into MySQL and then sorting them, PROBLEM...LAST CHANCE


strujillo

Recommended Posts

so i am trying to add the date into my mysql database, but no matter what i do it wont work. If tried

$today=date('F j, Y h:i');

 

But that didnt work, well it did but i didnt have mysql set to the DATETIME function thingy so when i tried to sort it it wouldnt sort them correctly.

 

 

Here is the code that im using

<?php
//This code checks to make sure they are logged in BEFORE they can access the webpage
session_start();
if(!isset($_SESSION['myusername'])){
//If he is not logged in, go to index.php
header("location:/si/onikz/index.php");
}else{
//If he is logged in do nothing and go on to the rest of the code.
}

include("../housekeeping/dbconnect.php"); 

$to = $_POST['to'];
$from = $_SESSION['myusername'];
$subject= $_POST['Subject'];
$message= $_POST['Message'];
$today=date('F j, Y h:i');


$results="INSERT INTO `SocInt`.`email` (
	`id` ,
	`to` ,
	`from` ,
	`subject` ,
	`message`,
	`date`
	)
	VALUES (
	NULL, '$to', '$from', '$subject', '$message', '$today' 
	);";

if(mysql_query($results))
{
	echo"<center>";
	echo"Your message was successful!<br/>";
	echo "<meta http-equiv=\"Refresh\" content=\"1; URL=http://onikz.com/si/email/index.php?inbox=Inbox\">";
	echo "<a href=\"http://onikz.com/si/email/index.php?inbox=Inbox\">Click here if you are not automatically redirected</a>";
	echo"</center>";
}else{echo"Sorry, an error occured, please contact the administrator or try again." . mysql_error();}
?>

 

When i go to the inbox section of the mail all of the dates are listed as 00000000 :( sniffle

 

Also when i try and sort them using this code

			$sql ="SELECT * FROM `SocInt`.`email` WHERE `to`='$username' ORDER BY   'date' DESC"; 

 

Will they sort from newest to oldest

I believe the problem is in your date format. If you want to just insert the current timestamp, I believe you can use the SQL NOW() function in the query. If you still want to use PHP, I believe it wants it in this format: YYYY-MM-DD HH:MM:SS

 

or:

<?php
date('Y-m-d H:i:s');
?>

Archived

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

×
×
  • 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.