Dreamweaver help: using PHP/MySQL formatting date | Newbie | | Join Date: Mar 2008
Posts: 20
| |
hello,
On a dynamic html page I have a table with a date column - <td><?php echo $row_resultado_viajes['fecha']; ?> </td>
I want the date to be displayed in other format so I am using the date function - <td><?php echo date("j-m-Y",$row_resultado_viajes['fecha']); ?> </td>
but I am getting a strange date echoed (31-12-1969). The day seems to be correct but the month and year are far from being right.
thanks,
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Dreamweaver help: using PHP/MySQL formatting date
What exact format (type and content) is the variable $row_resultado_viajes['fecha'].
As you know, date() works with a format description (1st parm) and the time in integer (2nd parm).
Ronald
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Dreamweaver help: using PHP/MySQL formatting date
When that field is a database date field in MySQL DATE format (YYYY-MM-DD), then convert the database date field to integer and then reformat that to the desired output format, like[php]
echo date("j-m-Y", strtotime($row_resultado_viajes['fecha'));[/php]Ronald
| | Newbie | | Join Date: Mar 2008
Posts: 20
| | | re: Dreamweaver help: using PHP/MySQL formatting date Quote:
Originally Posted by ronverdonk When that field is a database date field in MySQL DATE format (YYYY-MM-DD), then convert the database date field to integer and then reformat that to the desired output format, like[php]
echo date("j-m-Y", strtotime($row_resultado_viajes['fecha'));[/php]Ronald Thanks ronverdonk! it works fine now :)
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Dreamweaver help: using PHP/MySQL formatting date rodrigo never said that this field comes from MySQL. Could be Oracle, DB/2 or even Access.
And, by the way, this is the PHP forum, not the MySQL forum.
Ronald
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Dreamweaver help: using PHP/MySQL formatting date Quote:
Originally Posted by rodrigo21 Thanks ronverdonk! it works fine now :) You are welcome. See you next time.
Ronald
| | Newbie | | Join Date: Mar 2008
Posts: 20
| | | re: Dreamweaver help: using PHP/MySQL formatting date
Yes, indeed I tried a lot of time to do it but I am a newbie in this and could not code it correctly in my dreamweaver code edit I now the code should be something like this (for example)
SELECT DATE_FORMAT(fecha, '%W %M %Y');
but don't know how to open a mysql code in dreamweaver code editor for this pourpuse.
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Dreamweaver help: using PHP/MySQL formatting date Quote:
Originally Posted by rodrigo21 Yes, indeed I tried a lot of time to do it but I am a newbie in this and could not code it correctly in my dreamweaver code edit I now the code should be something like this (for example)
SELECT DATE_FORMAT(fecha, '%W %M %Y');
but don't know how to open a mysql code in dreamweaver code editor for this pourpuse. The correct MySQL format for this query would be in your case: - select date_format(fecha, "%d-%m-%Y") as fecha;
Ronald
| | Newbie | | Join Date: Mar 2008
Posts: 20
| | | re: Dreamweaver help: using PHP/MySQL formatting date Quote:
Originally Posted by ronverdonk The correct MySQL format for this query would be in your case: - select date_format(fecha, "%d-%m-%Y") as fecha;
Ronald ok, but hoe should I insert that mysql statement into my page code? I tried with:
mysql>.........
mysql = ......
sql =.......
but now success.
thanks
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Dreamweaver help: using PHP/MySQL formatting date
Since I don't use Dreamweaver (it's much nicer to write your own code) I have no idea how MySQL fits in it. However, I can show you the PHP code that would be used to access the MySQL database.
How you do that in DW I have no idea, but I will change the thread title a bit and let's hope that some DW guru sees it and can help you further.
The PHP code would be someting like[php]// Make a MySQL Connection
$conn = mysql_connect("localhost", "userid", "password")
or die("Could not connect to the database server: ".mysql_error());
// select the database
mysql_select_db("db_name", $conn)
or die("Could not select the database: " . mysql_error());
// construct your SQL statement
$sql="SELECT DATE_FORMAT(fecha, "%d-%m-%Y") as fecha FROM table_name WHERE your_condition";
// execute the sql statement
$result=mysql_query($sql)
or die("SELECT error: ".mysql_error());
// no rows selected: exit with message
if (mysql_num_rows < 1)
die ("No rows selected");
// fetch the selected row
$row_resultado_viajes = mysql_fetch_assoc($result);
// Here follows the code you showed at the start of the thread
?>
<td><?php echo $row_resultado_viajes['fecha']; ?> </td>[/php]Ronald
| | Member | | Join Date: Jul 2006
Posts: 92
| | | re: Dreamweaver help: using PHP/MySQL formatting date Quote:
Originally Posted by ronverdonk rodrigo never said that this field comes from MySQL. Could be Oracle, DB/2 or even Access.
Ronald Read the thread title old man
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,938
| | | re: Dreamweaver help: using PHP/MySQL formatting date Quote:
Originally Posted by aktar Read the thread title old man Not sure the 'old man' was necessary.
And i'm pretty sure the thread title has been changed from what it was originally.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,439 network members.
|