Jump to content

Format dates in php


kahodges

Recommended Posts

I am having some trouble getting the code below to pull dates from a database and echo:

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT nickname, last_name, date_format(BirthDate,"%m/%d/%Y"), QuitDate FROM employees WHERE QuitDate is null AND MONTH(BirthDate) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"nickname");
$f2=mysql_result($result,$i,"last_name");
$f3=mysql_result($result,$i,"date_format(BirthDate,"%m/%d/%Y")");
?>

 

It throws the error: Parse error: syntax error, unexpected '%'. But, the above query is valid and works in phpMyAdmin. Any help would be appreciated.

Thanks in advance.

Link to comment
Share on other sites

"date_format(BirthDate,"%m/%d/%Y")"
^- start string
                       ^- end string
                                ^- start string
                                  ^- end string

 

What you have is a MySQL string enclosed in a PHP string.  When you start doing these types of things you have to be careful of how you nest strings.

 

Either of these might work:

"date_format(BirthDate,\"%m/%d/%Y\")"
'date_format(BirthDate,"%m/%d/%Y")'

 

Taking it further, your original code is exactly the same, syntactically, as if you had typed:

"string1" %m/%d/%Y "string2"

Which is not valid PHP code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.