|
-
Oct 9th, 2010, 06:06 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Format date for report
i have this and its in YYYY-MM-DD format now.
Code:
<td><font size="1" face="Arial, Helvetica, sans-serif"><?php echo $callindate; ?></font></td>
i need to format the callindate just for the report in the echo command to MM/DD/YYYY
-
Oct 9th, 2010, 09:53 PM
#2
Re: Format date for report
You could use string processing...
PHP Code:
function changeDate($d){ $dateParts = explode('-',$d); return $dateParts[1].'/'.$dateParts[2].'/'.$dateParts[0]; } echo changeDate($callindate);
...or the DateTime object (available in PHP 5.2+)...
PHP Code:
function changeDate($d){ $dt = new DateTime($d); return $dt->format('m/d/Y'); } echo changeDate($callindate);
I usually store dates as a timestamp, as that's often the easiest to reformat using just the date() function.
Last edited by SambaNeko; Oct 9th, 2010 at 09:56 PM.
-
Oct 10th, 2010, 09:23 AM
#3
Thread Starter
Hyperactive Member
Re: Format date for report
Samba,
i tried
Code:
function changeDate($d){
$dateParts = explode('-',$d);
return $dateParts[1].'/'.$dateParts[2].'/'.$dateParts[0];
}
<td><font size="1" face="Arial, Helvetica, sans-serif"><?php echo changeDate($callindate); ?></font></td>
and i get an error
Fatal error: Call to undefined function changedate() in /data/21/1/14/99/1992262/user/2178205/htdocs/ReportAllByUnit.php on line 231
i also tried the other function as well with the same result. I must be doing something wrong.
-
Oct 10th, 2010, 10:08 AM
#4
Re: Format date for report
if what you pasted is where you put the function definition, you seemed to have defined it in the HTML. if this is the case, you can't do that. functions are PHP code and must be defined in PHP. it would be best to put function definitions at the very top, or very bottom, of your script. or, put them in an include file and then include them as you need them.
PHP Code:
<?php function changeDate($d){ $dateParts = explode('-',$d); return $dateParts[1].'/'.$dateParts[2].'/'.$dateParts[0]; } ?> <td><font size="1" face="Arial, Helvetica, sans-serif"><?php echo changeDate($callindate); ?></font></td>
-
Oct 10th, 2010, 10:21 AM
#5
Thread Starter
Hyperactive Member
Re: Format date for report
kows,
i doing this
Code:
<?php
function changeDate($d){
$dateParts = explode('-',$d);
return $dateParts[1].'/'.$dateParts[2].'/'.$dateParts[0];
}
?>
<td><font size="1" face="Arial, Helvetica, sans-serif"><?php echo changeDate($callindate); ?></font></td>
and i get this error
Code:
Fatal error: Cannot redeclare changedate() (previously declared in /data/21/1/14/99/1992262/user/2178205/htdocs/ReportAllDesc.php:214) in /data/21/1/14/99/1992262/user/2178205/htdocs/ReportAllDesc.php on line 214
line 214 is the "function changedate($d){
i dont have the function anywhere else
-
Oct 10th, 2010, 03:53 PM
#6
Re: Format date for report
oh. then you did define it properly the first time. but, then there shouldn't be any reason for the script to complain the function doesn't exist. remove one of the declarations and try again?
-
Oct 10th, 2010, 08:42 PM
#7
Thread Starter
Hyperactive Member
Re: Format date for report
this did it
Code:
<?php
if(!function_exists('changeDate'))
{
function changeDate($d){
$dateParts = explode('-',$d);
return $dateParts[1].'/'.$dateParts[2].'/'.$dateParts[0];
}
}
?>
<td><center><font size="1" face="Arial, Helvetica, sans-serif"><?php echo changeDate($callindate); ?></font></center></td>
-
Oct 11th, 2010, 02:01 AM
#8
Re: [RESOLVED] Format date for report
.. that doesn't do anything, though, unless you're defining the function twice for some reason. just remove the second definition of the function; you only need to define it once.
-
Oct 11th, 2010, 05:05 AM
#9
Thread Starter
Hyperactive Member
Re: [RESOLVED] Format date for report
but where is the second declaration? what i have works but not sure why exactly.
-
Oct 11th, 2010, 09:51 AM
#10
Re: [RESOLVED] Format date for report
I don't know, I don't have your code. press CTRL+F in your text editor and find changeDate. it has to be there somewhere.
-
Oct 11th, 2010, 06:00 PM
#11
Thread Starter
Hyperactive Member
Re: [RESOLVED] Format date for report
I have a lot of php file with functions in them. it must be in one of those somewhere. thanks again for you help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|