PHP date problems

Status
Not open for further replies.

clm100

Ars Legatus Legionis
13,874
Subscriptor
I've been struggling with this PHP code for a while now. The snippet is here and the parser document is here.
Currently, it looks like
code:<HR>
Code:
<?php
$db = mysql_connect("localhost","bnc","****");
mysql_select_db("bnc",$db);
$result = mysql_query("SELECT date_format(day,%M %d, %Y) as day,announce as announce FROM inf WHERE(day=CURRENT_DATE)",$db);
while ($myrow = mysql_fetch_array($result)) {
printf ("<center><B>%s</B></center><p>%s</p>\n", $myrow["day"], $myrow["announce"]);
}
?>
Basically the issue is with the day=CURRENT_DATE and the date_format. If I don't format the date then it works fine, but the site looks like ass (2001-12-30). If I format the Date (December 30, 2001) then the day=CURRENT_DATE doesn't work.

EDIT: The reason for the day=CURRENT_DATE is because I only want to display the entries for today.

Help...

You can find me in #linux/#programming on openIRC, as well as AIM clm100. Responses here are appreciated as well.
 

clm100

Ars Legatus Legionis
13,874
Subscriptor
Hmm

Well, I've fixed a few problems, but some still exist...
code:<HR>
Code:
<?php
$db = mysql_connect("localhost","bnc","****");
mysql_select_db("bnc",$db);
$result = mysql_query("SELECT date_format(day,'%M %d, %Y') as now, day as day, announce as announce FROM inf WHERE(day=CURRENT_DATE)",$db);
echo mysql_error();
if ($result) {
//if ($myrow = mysql_fetch_array($result)) {
 while ($myrow = mysql_fetch_array($result)) {
 printf ("<center><b>%s</b></center><p>%s</p>\n", $myrow["now"], $myrow["announce"]);
}
}
else {
echo ("Sods to you, there are no announcements today");
}
?>
The two largest issues are the if statement and the date.

The if statement doesn't work. How do I set it so that if there's no data, it'll run the else? Now it just fails. The if line that's commented out seems to work, except that the while then ignores that line.

Right now, it puts the date above every item. How can I have it only put the date at the top? The obvious ways use up an announcement/row. Perhaps another $result type of thing would be neccesary?

Any advice would be greatly appreciated.
 

nottestuser

Ars Scholae Palatinae
1,049
Untested but this is the structure you want:

code:<HR>
Code:
<?php
$db = mysql_connect("localhost","bnc","****");
mysql_select_db("bnc",$db);
$result = mysql_query("SELECT date_format(day,'%M %d, %Y') as now, day as day, announce as announce FROM inf WHERE(day=CURRENT_DATE)",$db);
echo mysql_error();
if ($myrow = mysql_fetch_array($result)) {
	printf ("<center><b>%s</b></center>\n", $myrow["now"]);
	do{
		printf ("<p>%s</p>\n", $myrow["announce"]);
	}while ($myrow = mysql_fetch_array($result));
}
	else {echo ("Sods to you, there are no announcements today");
}<P>?><P>

and for future use:

code:<HR>
Code:
//In:  mySQL date
//Out: human formated date string like "12/31/1999"
function mysql_DateDisp($date=""){
        if (strlen($date) == 0) return date("n/j/Y");
        $ret = date("n/j/Y", strtotime($date . " 00:00:00"));
        if ($ret == "12/31/1969")
                return 0;
        else
                return $ret;
}//mysql_DateDisp<P>//In:  human formated date string like "12/31/1999"
//Out: mySQL format date string
function mysql_DateStore($date=""){
        if (strlen($date) == 0) return date("Y-m-d");
        $ret = date("Y-m-d", strtotime($date . " 00:00:00"));
        if ($ret == "1969-12-31")
                return 0;
        else
                return $ret;<P>}//mysql_DateStore<P>
 
The mysql_query() function returns a non-false value if the query is successfull, regardless of the number of rows the query returns, even zero. The mysql_query() function only returns false when the SQL query fails for some reason, and it indicates you should not attempt to use the result. Use the mysql_num_rows($result) function to get the number of rows returned by a query.
 
notestuser: the first code block you have is not quite right. Here's something slightly improved:

code:<HR>
Code:
<?php
$db = mysql_connect("localhost","bnc","****");
mysql_select_db("bnc",$db);<P>$query = "SELECT date_format(day,'%M %d, %Y') as now, day as day, announce as announce FROM inf WHERE(day=CURRENT_DATE)";<P>if ($result = mysql_query($query,$db))
{
    if (mysql_num_rows($result) > 0)
    {
        while ($myrow = mysql_fetch_array($result))
        {
            printf ("<center>%s</center><p>%s</p>\n", $myrow["day"], $myrow["announce"]);
        }
    }
    else
    {
        echo ("Sods to you, there are no announcements today");
    }
}
else
{
    echo mysql_error();
}
 

nottestuser

Ars Scholae Palatinae
1,049
Minstrel, pardon my asking but what's not right about it? I understand the difference between our versions. Basically you're explicitly checking for every possible state where as I'm just relying on the mysql_fetch_assoc to fail when $result is not a mysql result object. If it is, but has no records then mysql_fetch_assoc will just return false. The error messages you'll see from your code will look better but that's about the only difference I can see.

I'm so keen to hear your thoughts since this is a structure I type about 14 times a day so I've thought about it a bit.
 
Nottestuser: I'd be glad to share my thoughts. The first issue is that I believe I misread your code, so to say it was wrong was an error on my part. The code as written will indeed function as intended. What I thought you had written was the following (which was the same as what clm100 wrote before you):

code:<HR>
Code:
if ($result = mysql_query($query))
{
    // do something
}
else
{
    echo "there are zero records";
}

The above code is clearly incorrect as the expression in the if statement would evaluate to TRUE upon completion of any successful query and FALSE only upon error conditions as the return value of mysql_query() is FALSE or of the Resource type which always evaluates to TRUE. Thus, an error would result in a message that no records were selected, which is not an accurate message.

I was thrown partially by your loop structure, in which you simulate a while loop with a do...while loop inside an if statement. The end result is the same.
 

clm100

Ars Legatus Legionis
13,874
Subscriptor
Here's an interesting problem. At some point on a script I've written (actually a different one) I set the variable $day to today. Now, I want to set it for tommorow. (IE d+1 for mon-thursday, and d+3 on fridays). (Essentially for the next working day.)

Totally unrelated php/mySQL question... Eventually I'll be writting a database that'll link to files (one file per entry). Is the most effiecent way to do this simply to store a Relative URL? Anywho, thanks a million, and I'm sure i'll be back for more.
 
Here's an inteesting problem. At some point on a script I've written (actually a different one) I set the variable $day to today. Now, I want to set it for tommorow. (IE d+1 for mon-thursday, and d+3 on fridays). (Essentially for the next working day.)

This really depends on what format of date your $day variable contains. If it is a unix timestamp (# of seconds since some day in 1970), then it's a matter of simply adding 86400 to the value for each day you want. Date and time handling functions in PHP are not really comprehensive and sometimes have overlaping purposes (date() and strftime() anyone?). If you can get away with it, the easiest thing to do is change all dates to unix timestamps coming into PHP and then manipulate them that way, until you have to format them for output again. If timestamps won't work for you for some reason, you'll probably have to develop your own generic date and time handling system (somewhat tedious, but doable)

Totally unrelated php/mySQL question... Eventually I'll be writting a database that'll link to files (one file per entry). Is the most effiecent way to do this simply to store a Relative URL? Anywho, thanks a million, and I'm sure i'll be back for more.

That sounds like a reasonable way to go to me. Cool features for that, depending on how handy you want/need it to be, are having the script check against the actual contents of the directory to catch broken/old links in the DB and catch files not yet in the DB and give you the oportunity to add them.

Have fun :D
 
Status
Not open for further replies.