Page 1 of 1
change date format!
Posted: Wed Mar 18, 2009 4:52 am
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????
Re: change date format!
Posted: Wed Mar 18, 2009 5:05 am
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.
Re: change date format!
Posted: Wed Mar 18, 2009 5:09 am
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!
Re: change date format!
Posted: Wed Mar 18, 2009 5:24 am
by susrisha
In your code, do this..
Code: Select all
date('Y-m-d H:i:s',strtotime($row['thedate']));
instead of
$row['date'];
Re: change date format!
Posted: Wed Mar 18, 2009 5:40 am
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>';
}
Re: change date format!
Posted: Wed Mar 18, 2009 6:25 am
by rwahdan
Thanks all for the help. its working now

Re: change date format!
Posted: Tue Apr 07, 2009 3:29 am
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
Re: change date format!
Posted: Wed Apr 08, 2009 1:53 am
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?