change date format!

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
rwahdan
Forum Newbie
Posts: 23
Joined: Thu Feb 12, 2009 1:49 am

change date format!

Post by rwahdan »

ok, i ask the user for a date from date picker then i make a format date so that it will convert it to mysql format yyyy-m-d then i check the database for records. before printing the records i want to format the date again to show as m-d-Y because it doesnt look good for a user to see 2009-03-17 for example!!
while($row = mysql_fetch_assoc($result))
{
echo '<tr><td>'.$row['AttendanceID'].'</td><td>'.$row['thename'].'</td><td>'.$row['theposition'].'</td><td>'.$row['emp#'].'</td><td>'.$row['thenote'].'</td><td>'.$row['thedate'].'</td><td>'.$row['theday'].'</td><td>'.$row['time_in'].'</td><td>'.$row['time_out'].'</td><td>'.$row['break_hrs'].'</td><td>'.$row['reg_hrs'].'</td><td>'.$row['worked_hrs'].'</td><td>'.$row['work_description'].'</td></tr>';
}
how can i reformat "thedate" filed to look mm-dd-yyyy instead of default mysql results yyyy-mm-dd????
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: change date format!

Post by temidayo »

Try this functions:

Code: Select all

 
//Your date to mysql date
function date_to_mysql($date) {
    //if $date is not numeric, assume it's in textual format
    if (!is_numeric($date)) {
        $date=strtotime($date);
    }
    return date('Y-m-d H:i:s',$date);         
}
 
//mysql to your own date
function myDate($date){
//if $date is not numeric, assume it's in textual format
    if (!is_numeric($date)) {
        $date=strtotime($date);
    }
    return date('m-d-Y H:i:s',$date);
}
 
You may modify the functions to suit your purpose.
Last edited by temidayo on Wed Mar 18, 2009 6:02 am, edited 1 time in total.
rwahdan
Forum Newbie
Posts: 23
Joined: Thu Feb 12, 2009 1:49 am

Re: change date format!

Post by rwahdan »

Thanks but i am sorry to say that i am new to php! i believe the one iam interested in is the second function. but where to put that with my code! sorry again!
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: change date format!

Post by susrisha »

In your code, do this..

Code: Select all

 
date('Y-m-d H:i:s',strtotime($row['thedate']));
instead of 
$row['date'];
 
 
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: change date format!

Post by temidayo »

Your new code could look like this:

Code: Select all

 
function myDate($date){
 
//if $date is not numeric, assume it's in textual format
    if (!is_numeric($date)) {
        $date=strtotime($date);
    }
    return date('m-d-Y H:i:s',$date);
}//function myDate($date)
 
while($row = mysql_fetch_assoc($result))
{
echo '<tr><td>'.$row['AttendanceID'].'</td><td>'.$row['thename'].'</td><td>'.$row['theposition'].'</td><td>'.$row['emp#'].'</td><td>'.$row['thenote'].'</td><td>';
echo myDate($row['thedate']);
echo'</td><td>'.$row['theday'].'</td><td>'.$row['time_in'].'</td><td>'.$row['time_out'].'</td><td>'.$row['break_hrs'].'</td><td>'.$row['reg_hrs'].'</td><td>'.$row['worked_hrs'].'</td><td>'.$row['work_description'].'</td></tr>';
}
 
rwahdan
Forum Newbie
Posts: 23
Joined: Thu Feb 12, 2009 1:49 am

Re: change date format!

Post by rwahdan »

Thanks all for the help. its working now :)
larsen
Forum Newbie
Posts: 2
Joined: Tue Apr 07, 2009 3:20 am

Re: change date format!

Post by larsen »

With date() and strtotime() function you can easily concert date between PHP and MySQL date format.

Here is an example:
http://n1scripts.com/php_scripts/php_co ... ipts&id=21
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: change date format!

Post by temidayo »

I thought this thread was supposed to be closed.
The poster said it was working fine, so why bring the dead thread up again?
Post Reply