Jump to content

Update sets date back to 01/01/1970


Lisa23

Recommended Posts

Hi i have a form which display the date in the format that i want which is m/d/yyyy but then when i try update it sets the date back to 01/01/1970 help please this how the update script looks like

 

 

<?php //Get the key field to be amended$news_id = $_GET['news_id']; $news_date = date('Y-m-d', strtotime($_GET['new_date']));$subject = $_GET['subject'];$news_artical = $_GET['news_artical'];// check if there were any errors// check if there were any errors//$news_date = STR_TO_DATE('$news_date','%Y/%m/%d');$query = "UPDATE news SET  news_date = '$news_date', subject='$subject', news_artical='$news_artical' WHERE news_id = '$news_id'";mysql_query($query) or die(mysql_error()); // execute query print "<p>The following records has been updated:  </p>";$result = mysql_query($query) ;//if there was a problem - get the error message and go back if (!$result)  {     echo "There were errors :<br>". mysql_error();  }   else //OK, then the insertion was successful  {        //Create a new query to display the new row in a table    $query = "SELECT news_id, news_date, subject, news_artical, DATE_FORMAT(news_date, '%d/%m/%Y') as new_date FROM news WHERE news_id = '$news_id' ";    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());     echo "<table cellpadding=10 border=1>";      while($row = mysql_fetch_array($result)) {           echo "<tr>";      echo "<td>".$row["news_id"]."</td>";     echo "<td><strong>" .$row["new_date"]."</strong></td>";     echo "<td><strong>".$row["subject"]."</strong></td>";      echo "<td width='55%'>".$row["news_artical"]."</td>";     echo "</tr>";     } //End while    echo "</table>";    } //End Else insertion successful//End else successful Amendment     ?>

 

Link to comment
Share on other sites

The question is; What IS the contents of $_GET['new_date']; because if your using in within the strtotime() function, it actually is quite fussy on how the string is given, if it can't convert the string into an integer (which is what strtotime() does) it defaults to epoch (01/01/1970) So my advice is check HOW $_GET['new_date']; gets set in the first place, and check where it gets set, then try it in the function as a static string before you go dynamic...

 

Rw

Link to comment
Share on other sites

i dnt think so why do u think that do u think thats whats causing the problem?? i tried to change to $_GET['news_date'] but still doesnt solve the problem??

 

 

I asked because that seemed to be the naming convention you were using with the other variables, $_GET['news_id'], $_GT['news_artical'] . . .

Link to comment
Share on other sites

well i thought that strot would allow me to set the date in a format of how i wish cz before i couldnt display how i wanted but now that strot and this line allow me to display the date in any format i want the problem is when update it sets back to 01-01-1927

 

 

 $query = "UPDATE news SET  news_date = '$news_date', subject='$subject', news_artical='$news_artical' WHERE news_id = '$news_id'";

 

Link to comment
Share on other sites

Again, you're not going to get any help with this until you inform us what $_GET['new_date'] contains when the script is run.  We are not mind readers.  Put in an:

 

echo $_GET['new_date'] ;

 

Also --- what is the datatype of the news_date column in the database.  Something tells me that it's a timestamp.

Link to comment
Share on other sites

ohh yeah new date is this line u'll notice i have news_date  as new dare which comes from the form the other part is the action script of the this form

 

$query = "SELECT news_id, news_date, subject, news_artical, DATE_FORMAT(news_date, '%d/%m/%Y') as new_date FROM news WHERE news_id = '$news_id' ";

 

Link to comment
Share on other sites

this is the form script where new date is entered

 

<?php 

// query to get records 
$news_id = $_GET['news_id'] ; 


// create query to delete record 

$query = "SELECT news_id, news_date, subject, news_artical, DATE_FORMAT(news_date, '%d/%m/%Y') as new_date FROM news WHERE news_id = '$news_id' ";

//Run the query
$result = mysql_query($query);


//see if any rows were returned 
if (mysql_num_rows($result) > 0) {  // yes - Display Form
  $row = mysql_fetch_array($result); //Fetch the row
  

  //Display the form with original values 
?>        [/php

and this is the update action part of the form

[code=php:0]<?php 



//Get the key field to be amended
$news_id = $_GET['news_id']; 
$news_date = date('Y-m-d', strtotime($_GET['new_date']));
$subject = $_GET['subject'];
$news_artical = $_GET['news_artical'];


// check if there were any errors

// check if there were any errors


$query = "SELECT news_id, news_date, subject, news_artical, DATE_FORMAT(news_date, '%d/%m/%Y') as new_date FROM news WHERE news_id = '$news_id' ";


//$news_date = STR_TO_DATE('$news_date','%Y/%m/%d');

$query = "UPDATE news SET  news_date = '$new_date', subject='$subject', news_artical='$news_artical' WHERE news_id = '$news_id'";
mysql_query($query) or die(mysql_error()); 

Link to comment
Share on other sites

And in case you were wondering why, the basic format for a mysql date needs to be YYYY-MM-DD.  You were inadvertantly passing the '05/01/2010' which is not a valid mysql string constant for a date column.  The code that converted it does in fact work properly but because you did not actually pass the variable you constructed, mysql took that as an invalid date, and defaulted it to the 1970 date, which for a timestamp is the first possible date you can store.

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.