Connecting Tech Pros Worldwide Forums | Help | Site Map

Dreamweaver help: using PHP/MySQL formatting date

Newbie
 
Join Date: Mar 2008
Posts: 20
#1: Mar 29 '08
hello,

On a dynamic html page I have a table with a date column

Expand|Select|Wrap|Line Numbers
  1. <td><?php echo $row_resultado_viajes['fecha']; ?>&nbsp; </td>
I want the date to be displayed in other format so I am using the date function

Expand|Select|Wrap|Line Numbers
  1. <td><?php echo date("j-m-Y",$row_resultado_viajes['fecha']); ?>&nbsp; </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,

ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Mar 29 '08

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
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#3: Mar 29 '08

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
Member
 
Join Date: Jul 2006
Posts: 92
#4: Mar 29 '08

re: Dreamweaver help: using PHP/MySQL formatting date


You could also format the date in your mysql query

see the following page:

http://dev.mysql.com/doc/refman/5.0/...on_date-format
Newbie
 
Join Date: Mar 2008
Posts: 20
#5: Mar 30 '08

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 :)
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#6: Mar 30 '08

re: Dreamweaver help: using PHP/MySQL formatting date


Quote:

Originally Posted by aktar

You could also format the date in your mysql query

see the following page:

http://dev.mysql.com/doc/refman/5.0/...on_date-format

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
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#7: Mar 30 '08

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
#8: Mar 30 '08

re: Dreamweaver help: using PHP/MySQL formatting date


Quote:

Originally Posted by aktar

You could also format the date in your mysql query

see the following page:

http://dev.mysql.com/doc/refman/5.0/...on_date-format


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.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#9: Mar 30 '08

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:
Expand|Select|Wrap|Line Numbers
  1. select date_format(fecha, "%d-%m-%Y") as fecha;
Ronald
Newbie
 
Join Date: Mar 2008
Posts: 20
#10: Mar 30 '08

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:

Expand|Select|Wrap|Line Numbers
  1. 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
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#11: Mar 30 '08

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']; ?>&nbsp; </td>[/php]Ronald
Member
 
Join Date: Jul 2006
Posts: 92
#12: Apr 1 '08

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
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,938
#13: Apr 1 '08

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.
Reply