Jump to content

Recommended Posts

Hi i've been trying to change the format of the date which comes from the database row [ news-date ]

this what i tried but di not work help please

 

$query = "SELECT * FROM news, date_format [news_date](date,'%M %d, %Y') AS news_date
"; 
$result = mysql_query($query); 

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
// yes so print them one after another 
  echo "<table cellpadding=10 border=1>"; 

  while($row = mysql_fetch_array($result))  {
  
   
  

     echo "<tr>"; 
     echo "<td>".$row["news_id"]."</td>"; 
     echo "<td>".$row["subject"]."</td>"; 
     echo "<td>" .$row['news_date']. "</td>";

     echo "<td>".$row["news_artical"]."</td>";
 echo "<td><a href=delete2.php?id=".$row["id"].">Delete</a></td>"; 
 echo "<td><a href=modifyform.php?id=".$row["id"].">Modify</a></td>";
 echo "<td><a href=add.php?id=".$row["id"].">Add Row</a></td>";
     echo "</tr>"; 
   } //End While
   echo "</table>"; 
} //End if rows 

	   echo "<p> <strong>{news.SUBJECT} </strong></p>"; 



?> 

Link to comment
https://forums.phpfreaks.com/topic/212709-how-can-i-change-the-format-of-the-date/
Share on other sites

try this as an experiment

 

$query "SELECT *, DATE_FORMAT(news_date, '%M %d, %Y') as new_date FROM news";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
echo $row['new_date'] . "<br>";
}

try this as an experiment

 

$query "SELECT *, DATE_FORMAT(news_date, '%M %d, %Y') as new_date FROM news";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
echo $row['new_date'] . "<br>";
}

 

didnt like this line

$query "SELECT *, DATE_FORMAT(news_date, '%M %d, %Y') as new_date FROM news";

 

didnt like this line

$query "SELECT *, DATE_FORMAT(news_date, '%M %d, %Y') as new_date FROM news";

 

missing = after  $ query

$query = "SELECT *, DATE_FORMAT(news_date, '%M %d, %Y') as new_date FROM news";

 

thank you very much it working thank you ;)

now the modify script sets the date back to 0000/00/00 on the scho i set it likeusing his sintax

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

how can i have the same way when update because now it sets the date back 0000/00/00

 

this what the update script looks like hwo can i make it so it doesnt set it back to  0000/00/00

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

what is the data type for your date field?

 

date on the database  the format is YYYYY/M/D

 

but because i set to display like d/m/yyyy using the

$query "SELECT *, DATE_FORMAT(news_date, '%M %d, %Y') as new_date FROM news";

when i use to update it returns 0000/00/00 on the database

how can i do the same on the on modify date

There's a complementary function to DATE_FORMAT() called STR_TO_DATE() - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date

 

However, since you should be validating the data you receive before putting it into a query, your php code could break apart (explode) the d/m/yyyy values and put them together in the yyyy-mm-dd format before you put it into a query.

yah now now i want to do the other way i want just send back to database but because the format on the database is y m d and on the query i have d m y when i try to up date i want to send modified so that it fits back databse criteria

 

i tried like this but did not work help please

 

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

simple example

$my_date_01 = "3/13/2010"; /* the date in m/d/yyyy format */
$my_date_array = explode("/", $my_date);
$my_date_02 = $my_date_array[2] . "/" . $my_date_array[0] . "/" . $my_date_array[1];
/* $my_date_02 now looks like 2010/3/13 */

 

simple example

$my_date_01 = "3/13/2010"; /* the date in m/d/yyyy format */
$my_date_array = explode("/", $my_date);
$my_date_02 = $my_date_array[2] . "/" . $my_date_array[0] . "/" . $my_date_array[1];
/* $my_date_02 now looks like 2010/3/13 */

 

how do i implement that on my update

 

$query = "UPDATE news SET news_date = STR_TO_DATE('$mynewdatevalue','%Y/%m/%d'), subject='$subject', news_artical='$news_artical' WHERE news_id = '$news_id'";

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.