Need help with display date

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
crowegreg
Forum Newbie
Posts: 13
Joined: Tue Jul 19, 2011 2:48 pm

Need help with display date

Post by crowegreg »

Hello, I'm needing assistance in displaying a date that is being retrieved from a mysql DB. Here's what I have so far:

while($row2 = mysql_fetch_array($result2)) {
echo "<tr>";
echo "<td>" . $row2['date'] . "</td><td>" . $row2['Notes'] . "</td>";
echo "</tr>";
echo "<br />";
}

All of this works, but my date is displayed as 2011-07-21. I'd like for it to be displayed as either 07-21-2011 or 07/21/2011.
Thanks for your assistance!!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Need help with display date

Post by social_experiment »

In your sql query you can use DATE_FORMAT().

Code: Select all

<?php
 $query = "SELECT DATE_FORMAT(dateField, '%m-%d-%Y')";
?>
Should display month-day-year (%d-%m-%Y will return day-month-year)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
crowegreg
Forum Newbie
Posts: 13
Joined: Tue Jul 19, 2011 2:48 pm

Re: Need help with display date

Post by crowegreg »

Thanks for suggestion. If this is my original sql statement:
$sql2 = "SELECT wp_ebyz_card_usage.date, wp_ebyz_card_usage.Notes
FROM wp_ebyz_card_usage
WHERE wp_ebyz_card_usage.cardID='{$_POST[CardNumber]}' AND wp_ebyz_card_usage.merchantID='{$_SESSION['merchantid']}' ORDER BY wp_ebyz_card_usage.date DESC";

How do I add your suggestion to this statement?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Need help with display date

Post by social_experiment »

Code: Select all

<?php
...DATE_FORMAT(wp_ebyz_card_usage.date, '%m-%d-%Y')...
?>
You have to modify the field that contains the date you want to use like the example above, it is simply added into the query
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply