changing format of timestamp

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

changing format of timestamp

Post by bruceg »

I have a timestamp which displays as the default.

you can check it out here:

http://www.inspired-evolution.com/displ ... esses2.php

the php is <td>{$row['last_updated']}</td>

and last_updated is a column set as 'timestamp' in MySQL.


I would rather it be formatted as just Month, Day. year.

can this be done?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Code: Select all

date() getdate()
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

o.k, so the PHP syntax would be :

$today = date("m.d.y");

where would that need to go in the PHP code pulling from MySQL table

Code: Select all

echo<<<TABLETOP
<table id="contact_database" cellspacing="0">
<tr>
   <th>Name</th>
    <th>Primary Telephone</th>
    <th>Secondary Telephone</th>
    <th>Email Address</th>
    <th>Notes</th>
	<th>Last Updated</th>
	</tr>
	
TABLETOP;

while ($row = mysql_fetch_assoc($result)) {  // Display the data from mysql
echo<<<TABLEDATA
  <tr>
    <td class="bold">{$row['Name']}</td>
    <td>{$row['primary_tel']}</td>
    <td>{$row['2ndary_tel']}</td>
    <td><a href="mailto:{$row['email_address']}">{$row['email_address']}</a></td>
    <td>{$row['Notes']}</td>
	<td>{$row['last_updated']}</td>
</tr>

TABLEDATA;

}  //  End while

echo "</table>";

}  // End if


?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

bruceg wrote:o.k, so the PHP syntax would be :

$today = date("m.d.y");

where would that need to go in the PHP code pulling from MySQL table

Code: Select all

echo<<<TABLETOP
<table id="contact_database" cellspacing="0">
<tr>
   <th>Name</th>
    <th>Primary Telephone</th>
    <th>Secondary Telephone</th>
    <th>Email Address</th>
    <th>Notes</th>
	<th>Last Updated</th>
	</tr>
	
TABLETOP;

while ($row = mysql_fetch_assoc($result)) {  // Display the data from mysql
$today = date("m.d.y", $row['last_updated']);
echo<<<TABLEDATA
  <tr>
    <td class="bold">{$row['Name']}</td>
    <td>{$row['primary_tel']}</td>
    <td>{$row['2ndary_tel']}</td>
    <td><a href="mailto:{$row['email_address']}">{$row['email_address']}</a></td>
    <td>{$row['Notes']}</td>
	<td>{$today}</td>
</tr>

TABLEDATA;

}  //  End while

echo "</table>";

}  // End if


?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You can do the formatting while it's still in the dbms... Lookup the DATE_FORMAT function in the MySQL manual...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

If your date is stored as a MySQL timestamp in the database, it's going to be in a different format that the UNIX timestamp date() expects. To fix this, don't just modify your query a bit:

Code: Select all

...
, UNIX_TIMESTAMP(last_updated) as last_updated
...
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply