Jump to content

strftime problem


tommr

Recommended Posts

I use

strftime('%b %d, %Y', strtotime($row['start_date']));

in several places and it works fine.

 

When I take this code

 

{

echo "<a href=\"/show_submits/show_detail.php?id="  $row['id']  "\">Details</a>\n";

echo $row['venue_state']  "        "  $row['start_date']  "        "  $row['show_name']  "        "  $row['venue_city']  "<br>\n";

}

 

and change it to this code

 

{

echo "<a href=\"/show_submits/show_detail.php?id="  $row['id']  "\">Details</a>\n";

echo $row['venue_state']  "        "  strftime('%b %d, %Y', strtotime($row['start_date']));  "        "  $row['show_name']  "        "  $row['venue_city']  "<br>\n";

}

 

I get this error.

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/arts/public_html/shows/index.html on line 194

 

The only change I made was for the $row['show_name']

Am I making an obvious mistake?

 

Link to comment
Share on other sites

Both code snippets should give you errors, since you're not concatenating the string elements.

 

Your original code:

<?php
{
echo "<a href=\"/show_submits/show_detail.php?id="  $row['id']  "\">Details</a>\n";
echo $row['venue_state']  "        "  $row['start_date']  "        "  $row['show_name']  "        "  $row['venue_city']  "<br>\n";
}
?>

can be written as

<?php
{
echo "<a href='/show_submits/show_detail.php?id={$row['id']}'>Details</a>\n";
echo $row['venue_state'] . "       {$row['start_date']}         {$row['show_name']}       {$row['venue_city']}<br>\n";
}
?>[code]

Ken

Link to comment
Share on other sites

For example, the first line

echo "<a href=\"/show_submits/show_detail.php?id="  $row['id']  "\">Details</a>\n";

 

should be written

echo "<a href=\"/show_submits/show_detail.php?id=\"{$row['id']}\">Details</a>\n";

 

You can't simply toss a quote into the middle of a string and insert a variable. The quotes within the same type of quotes have to be escaped, and variables aren't interpolated within single quoted strings. Very briefly:

 

$variable = 'another string';

echo "This is a string with $variable inside";

echo 'This is a string with ' . $variable . ' inside';

Both of the above will return This is a string with another string inside

 

echo 'This is a string with $variable inside';

That will return This is a string with $variable inside

 

Array elements are a bit different. To use them in a double quoted string, they need to be enclosed in curly braces {}, or concatenated.

echo "This is an {$array['element']} in a string.";

OR

echo "This is an " . $array['element'] . "in a  string".

 

As for quotes, to echo a double quote from within a double quoted string, or a single quote from within a single uoted string, it must be escaped with a backslash.

 

echo "This \"string\" is valid";

echo 'This "string" is valid';

 

echo "This "string" is not valid";

 

echo 'This \'string\' is valid';

echo "This 'string' is valid";

 

echo 'this 'string' is not valid';

Link to comment
Share on other sites

The date is stored as 2010-12-10

I want to display the date as Dec 10, 2010

 

I used the strftime('%b %d, %Y', strtotime($row['start_date'])) on other parts of the site and it worked with out noticeable trouble.

Here is an example...

http://www.artsandcraftsnetwork.com/show_submits/6ny.php

 

Now if I go to

http://www.artsandcraftsnetwork.com/shows/

and search for ny I get the list but with the 2010-12-10

 

The database version is too confusing.

 

The whole blasted thing is a Frankenstein of parts and pieces I bought, wrote and had other help with.

I tried to find an events script that would do what I wanted but none was available and I do not have the funds to commission one so there you go.

 

Any help would be appreciated.

 

 

Link to comment
Share on other sites

No errors but nothing is printing for date.

I am using...

 

$sql = "select * from craft_shows DATE_FORMAT(`start_date`, '%b %e, %Y') AS s_date ";

 

 

echo "<a href='/show_submits/show_detail.php?id={$row['id']}'>Details</a>\n";

echo $row['venue_state'] . "       {$row['s_date']}         {$row['show_name']}       {$row['venue_city']}<br>\n";

 

 

Link to comment
Share on other sites

You aren't actually executing the query in that code, and since the s_date is derived FROM the table, it needs to be before the FROM parameter in the query string.

 

$sql = "SELECT *, DATE_FORMAT(`start_date`, '%b %e, %Y') AS s_date FROM craft_shows";
$result = mysql_query($sql);
while( $row = mysql_fetch_assoc($result) ) {
echo "<a href='/show_submits/show_detail.php?id={$row['id']}'>Details</a>\n";
echo $row['venue_state'] . "       {$row['s_date']}         {$row['show_name']}       {$row['venue_city']}<br>\n";
}

Link to comment
Share on other sites

Thank you thank you.

It works but by the looks of the results I need to sort by date!

 

One of the problem of working from a book is that if I have questions there is no one to help.

Thanks to your help I now understand the concept of FROM.

This is a big help in getting that detail nailed down but I really appreciate your taking the time to show what I am doing wrong.

 

I think I will spend some time browsing the forum for more ideas of how to make the site work better.

 

 

 

Link to comment
Share on other sites

You can add an ORDER BY clause to the query as well.

 

$sql = "SELECT *, DATE_FORMAT(`start_date`, '%b %e, %Y') AS s_date FROM craft_shows ORDER BY start_date DESC";

 

MySQL has some very good native date/time manipulation functions, it's just a matter of knowing they exist, and how to use them.

Link to comment
Share on other sites

I can figure this out.

I thought it was working but for what ever reason this code calls all the results no matter I select from the drop down menu.

If I take the date and code fix out it works on but the date is not displayed as I want.

I have tried 6 ways to Sunday and no dice.

 

 

<?php

 

$find  = trim($_GET['find']);

$field = $_GET['field'];

 

 

if($find && $field) { // we have search form submitted

// first of all we need to check for values to prevent sql injection

$valid_fields = array("venue_state", "venue_city", "start_date");

if(!in_array($field, $valid_fields)) die("Error: Invalid field!");

 

// connect to our Database

mysql_connect("localhost", "arts_cshow", "My Password") or die(mysql_error());

mysql_select_db("arts_shows") or die(mysql_error());

echo "<h6>Search Results for $find </h6>\n";

 

$find = addslashes($find);

$result = mysql_query("SELECT * FROM craft_shows WHERE $field LIKE '%$find%'");

if(mysql_num_rows($result) == 0) {

echo "<p>0 matches found.</p>";

}

else {

$id = $row['id'];

echo "<table class=\"stshows\"><tr><td class=\"stshows\">";

while($row = mysql_fetch_array($result))

$sql = "SELECT *, DATE_FORMAT(`start_date`, '%b %e, %Y') AS s_date FROM craft_shows";

$result = mysql_query($sql);

while( $row = mysql_fetch_assoc($result) ) {

 

echo "<a href='/show_submits/show_detail.php?id={$row['id']}'>Details</a>\n";

 

echo $row['venue_state'] . "       {$row['s_date']}         {$row['show_name']}       {$row['venue_city']}<br>\n";

}

echo "</td></tr></table>\n";

}

}

?>

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.