Format date to MM-DD-YYYY in php and HTML

Hi all.

Below is a snippet of code from my php file. I retrieve the description and date from the mySQL table. The date is in the format YYYY-MM-DD, but when I display it in my html table I want it to be in the formath MM-DD-YYYY. How can I do this?

Thank you in advance!

 
<?php
$query = "SELECT  workscheduledescription, WorkScheduleDate FROM WorkScheduleDataEntry INNER JOIN WorkScheduleType ON WorkScheduleDataEntry.workscheduletypeid = WorkScheduleType.workscheduletypeid WHERE DataEntryID = :dataentryid ORDER BY WorkScheduleDate ";
        
            $query_params = array( 
            ':dataentryid' => $_POST['DataEntryID'] 
            );   
     
            $stmt = $db2->prepare($query); 
            $result = $stmt->execute($query_params); 
            $data2 = $stmt->fetchAll();
?>

Open in new window


Then in my html portion I have:

<table cellpadding="0" cellspacing="0"  class="db-table" align = "center">
                <tr>
                <th>Work</th>
                <th>Scheduled Date</th>
                </tr>
                               
                <?php  foreach($data2 as $row){ 
                    echo '<tr>';
                    echo '<td>' . html_escape($row['workscheduledescription']) . '</td>';
                    echo '<td>' . html_escape($row['WorkScheduleDate']) . '</td>';
                    echo '</tr>';
                   
                } ?>                 
               </table>

Open in new window

Sim1980Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Cornelia YoderArtistCommented:
The easiest way is simply to use substr().

$mydate = '2012-10-20';

echo substr($mydate,5,5)."-".substr($mydate,0,4);
0
Cornelia YoderArtistCommented:
If you might later want the html table date in a more human friendly format, you can also use

$unixtimestamp = strtotime($mydate);
echo  date("Y-m-d",$unixtimestamp);

and use the features of date() to format it in any way you wish.

http://us2.php.net/manual/en/function.date.php
0
Ray PaseurCommented:
This and much more is explained in the article here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

The advantage of using strtotime() and date() versus just reformatting the fields with substr() is that strtotime() can tell you if the date is bad.  Also, strtotime() and date() can be used together to do date-related arithmetic.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

GaryCommented:
You can also do it direct in your sql

STR_TO_DATE(date_column, '%d-%m-%Y')
0
Sim1980Author Commented:
Thank you both.
0
Ray PaseurCommented:
Lots of ways to skin a cat ;-)

If you want MM-DD-YYYY you might want to change this...

echo  date("Y-m-d",$unixtimestamp);

to this...

echo  date("m-d-Y",$unixtimestamp);
0
Cornelia YoderArtistCommented:
Could you please explain why you gave two other people all the points and didn't even bother to thank me for giving you the same answer first?
0
Sim1980Author Commented:
Sure. Your answer didn't guide me to where I wanted to go as far as a solution, while the other 2 answers did.
0
Cornelia YoderArtistCommented:
And what was that, since my answer was the same as Ray_Paseur's, except that I gave it to you directly, while ray pointed you to a link?  If giving you a link is better, I believe I did that as well.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.