Jump to content

Date Format


feddie1984

Recommended Posts

I have the below code and would like a way to display the output as dd/mm/yyyy instead of yyyy-mm-dd as it is stored in the MySQL Database.  Also if I could get some help formatting dd/mm/yyyy as yyyy-mm-dd from a form input so that I can add it to the database.

 

<?php
$dbhost = "";
$dbuser = "";
$dbpass = "";
$dbname = "";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$from = $_GET['from'];
$to = $_GET['to'];
// Escape User Input to help prevent SQL Injection
$from = mysql_real_escape_string($from);
$to = mysql_real_escape_string($to);
//build query
$query = "SELECT * FROM ajax_example1 WHERE Date_Format(ae_date, '%d/%m/%Y') between '$from' and '$to'";

//Execute query
$qry_result = mysql_query($query) or die(mysql_error());

//Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Date</th>";
$display_string .= "</tr>";

// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
$display_string .= "<tr>";
$display_string .= "<td>$row[ae_name]</td>";
$display_string .= "<td>$row[ae_date]</td>";
$display_string .= "</tr>";

}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
?>

Link to comment
https://forums.phpfreaks.com/topic/198640-date-format/
Share on other sites

I have tried that but here:

	// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
$display_string .= "<tr>";
$display_string .= "<td>$row[ae_name]</td>";
$display_string .= "<td>echo date("d/m/Y", strtotime($row['ae_date']))</td>";
$display_string .= "</tr>";

 

Which gives the error:

 

Parse error: syntax error, unexpected T_STRING in /home/tburgess/public_html/socomec/Ajax_Test/ajax-example1.php on line 33

 

Can you help me with the correct syntax?

 

Link to comment
https://forums.phpfreaks.com/topic/198640-date-format/#findComment-1042412
Share on other sites

All sorted, it's been a long week thanks for our help!!  :)

 

	// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
$display_string .= "<tr>";
$display_string .= "<td>$row[ae_name]</td>";
$display_string .= "<td>" . date("d/m/Y", strtotime($row['ae_date'])) . "</td>";
$display_string .= "</tr>";

}

Link to comment
https://forums.phpfreaks.com/topic/198640-date-format/#findComment-1042417
Share on other sites

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.