Forum Moderators: coopster

Message Too Old, No Replies

View results where date less than 14 days old

         

brodie_r

5:34 am on Feb 19, 2006 (gmt 0)

10+ Year Member



Currently when i add reports i input the date in format DD/MM/YYYY. I then have two different view reports pages. One i want to show reports for the present day, and one for all reports in the past 14 days.

The command i am using for the 14 days is this, but i added a report yesterday and still not getting any results.

$query ="SELECT * FROM jobs WHERE date > DATE_SUB(now(), INTERVAL 14 DAY)";

Can someone see where i am going wrong.

DrDoc

5:57 am on Feb 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



DATE() is a function. You will need to backtick the field name.

brodie_r

6:29 am on Feb 19, 2006 (gmt 0)

10+ Year Member



Sorry, but what does back tick mean?

DrDoc

6:43 am on Feb 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The backtick
`
is used to escape field/table names which are using reserved words or functions as their name. In your case,
date
is a function. Thus, instead of refering to the field as
date
you need to use
`date`
in your queries.

For more information: [dev.mysql.com...]

DrDoc

7:10 am on Feb 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh, and you should probably use CURDATE() instead of NOW()

brodie_r

7:11 am on Feb 19, 2006 (gmt 0)

10+ Year Member



I tried the statement:
[codes]
$query ="SELECT * FROM jobs WHERE `date` > DATE_SUB(now(), INTERVAL 14 DAY)";
[/codes]

but still having problems, so im going to give you all the details so maybe then someone can help.

I have a table called jobs. Where there is a field called date. The date field is just a standard VARCHAR, and i input the date as 02/19/2006 (MM/DD/YYYY).

Please ask if you need any more information

DrDoc

7:13 am on Feb 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aha! VARCHAR ... That means you need to run a function to convert the string to a date in YYYY-MM-DD format (or YYYYMMDD, depending on whether you want to do string or integer comparison)

$query ="SELECT * FROM jobs WHERE STR_TO_DATE(`date`, '%m/%d/%Y') > DATE_SUB(CURDATE(), INTERVAL 14 DAY)";

brodie_r

7:21 am on Feb 19, 2006 (gmt 0)

10+ Year Member



came back with an error saying:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(`date`, '%m/%d/%Y') > DATE_SUB(CURDATE(), INTERVAL 14 DAY)' at

coopster

12:54 pm on Feb 20, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It may be the version of MySQL you are running ... STR_TO_DATE() is available as of MySQL 4.1.1.

Also, although both points have been recognized here, I would take them one step further. If you aren't too far along in your development and can easily change that table without having to rewrite many lines of code, I would do two things --

  1. Change that column name -- DO NOT use reserved words [dev.mysql.com] as column names. Sure, a reserved word can be used as an identifier if you quote it, but why even put yourself in that situation? I think you'll find life much easier if you just don't use reserved words as column identifiers.
  2. Change the column type while you are at it to a DATE type column. Then you won't find yourself converting DATE data all the time just to use MySQL's Date and Time Functions [dev.mysql.com]. If this column truly does store only DATE data, use a DATE column type [dev.mysql.com].

brodie_r

1:19 pm on Feb 20, 2006 (gmt 0)

10+ Year Member



Thanks for your suggestions coopster. I have actually been able to figure alot of that out, and when i finish will produce my end result. I did infact already change my column type to DATE as it fixed a few problems. The problem i have now, is instead of echo the date as YYYY-MM-DD i want to be able to have Monday 1st 2006 etc when echo'd.

So the code i wrote for this was:
<?php echo date("l, F d, Y", ' . $date . ');?>

I keep just getting 31 December 1969. Which after some research have found this is caused when the date() cant find a usable date, ie. empty. How come it sais its empty when $date definetly exists. As when i do echo $date it works fine, only when trying to convert to words it mucks up.

DrDoc

1:27 pm on Feb 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's because PHP's date() function expects the date as a unix timestamp.

brodie_r

1:33 pm on Feb 20, 2006 (gmt 0)

10+ Year Member



I have read every page i can find, and still cant get my head around this to fix it

coopster

1:36 pm on Feb 20, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Your syntax there doesn't look right, you are passing an invalid value to the date() function as the second argument. You need to
echo date("l, F d, Y", $date);
perhaps?

brodie_r

1:42 pm on Feb 20, 2006 (gmt 0)

10+ Year Member



I think DrDoc is on the right track, but how can i convert my YYYY-MM-DD date into a readable unix timestamp so that it can be read in date()

coopster

2:05 pm on Feb 20, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could break it apart and use mktime() [php.net] to get a timestamp, but let me ask you this, is this the date that is coming from your MySQL database table result set? If so, why not just let the database do the formatting work?

brodie_r

2:07 pm on Feb 20, 2006 (gmt 0)

10+ Year Member



What i have is a javascript calendar that enters the date as YYYY-MM-DD. So are you saying their is a better way of doing this?

brodie_r

2:21 pm on Feb 20, 2006 (gmt 0)

10+ Year Member



Ok i figured this out using EXPLODE and MKTIME:

<?php $date2 = explode("-", $date);?>
<?php $year = $date2[0];?>
<?php $month = $date2[1];?>
<?php $day = $date2[2];?>

<?php echo date("l, F d, Y", mktime($year, $month, $day));?>

coopster

2:21 pm on Feb 20, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I was just wondering if the date was coming from your database. If it was you could format it as you pull it out of the database. Example:
SELECT DATE_FORMAT('2006-02-20', '%W, %M %e, %Y'); 
-- will return:
-- Monday, February 20, 2006

But if the date is coming from user-supplied input you can use a number of options. One is to break the date into pieces which you pass to the mktime() function and return a timestamp which could then be passed to your date() function. There is also the strftime [php.net] function that could get the job done. Either way, since the data is user supplied you will want to validate the date first.
$date = '2006-02-20'; 
list($year, $month, $day) = explode('-', $date);
if (checkdate($month, $day, $year)) {
echo date("l, F d, Y", mktime(0,0,0,$month,$day,$year));
} else {
// bad date
}

posted at the same time, glad you got it sorted, brodie_r, but take a closer look at your mktime function, you need to pass those time arguments first!

[edited by: coopster at 2:24 pm (utc) on Feb. 20, 2006]

brodie_r

2:23 pm on Feb 20, 2006 (gmt 0)

10+ Year Member



ok i wouldnt really call it fixed, because the page returns multiple results, and now they are all returning the date as 15th may. :S heck im so lost now

brodie_r

2:32 pm on Feb 20, 2006 (gmt 0)

10+ Year Member



Coopster and anyone else following this thread.

Ill give you full details to how this is working. I have a page called view_jobs.php, this page lists all the jobs, and most jobs having different dates.

======= START OF CODE
<?php
include('result_top.inc');
require_once ('mysql_connect.php');

$query ="SELECT * FROM jobs WHERE `date` > DATE_SUB(CURDATE(), INTERVAL 14 DAY)";
$result = mysql_query($query);
$num=mysql_numrows($result);
if ($num > 0) {
}
if (!$result)
die(mysql_error());
while ($r = mysql_fetch_array($result)) {
extract($r);

$job_id=mysql_result($result,$i,"job_id");
$client=mysql_result($result,$i,"client");
$date=mysql_result($result,$i,"date");
$time=mysql_result($result,$i,"time");
$location=mysql_result($result,$i,"location");
$machine=mysql_result($result,$i,"machine");
$operator=mysql_result($result,$i,"operator");

$date2 = explode("-", $date);
$year = $date2[0];
$month = $date2[1];
$day = $date2[2];
?>

<span class="style3">
<tr>
<td><font face="Arial" size="2"><?php echo $job_id;?></font></td>
<td><font face="Arial" size="2"><?php echo $client;?></font></td>
<td><font face="Arial" size="2"><?php echo $date2;?></font></td>
<td><font face="Arial" size="2"><?php echo date("l, F d, Y", mktime($year, $month, $day));?></font></td>
<td><font face="Arial" size="2"><?php echo $time;?></font></td>
<td><font face="Arial" size="2"><?php echo $location;?></font></td>
<td><font face="Arial" size="2"><?php echo $machine;?></font></td>
<td><font face="Arial" size="2"><?php echo $operator;?></font></td>
<td><a href="view_job.php?id=<?php echo $job_id;?>"><img src="view.jpg" alt="View Job" width="12" height="16" border="0" /></a>   </td>
<td><a href="edit_job.php?id=<?php echo $job_id;?>"><img src="edit.png" alt="Edit Job" border="0" /></a>   </td>
<td><a href="javascript:confirmDelete('delete_job.php?id=<?php echo $job_id;?>')"><img src="delete.png" alt="Delete Job" width="16" height="16" border="0" /></a>
   </td>
<td><a href="print_job.php?id=<?php echo $job_id;?>"><img src="printer.jpg" alt="Print Job" width="19" height="17" border="0"></a></td>
</tr>
</span>

<?php
$i++;
}
echo "</table>";
?>
======= END OF CODE

coopster

4:28 pm on Feb 20, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK, so you are getting that date value from the database. Let's use MySQL then to format that date as opposed to retrieving it and formatting it with PHP. Yes, you could do it either way but let's have MySQL be the workhorse here.
$query = "SELECT 
*,
DATE_FORMAT(date, '%W, %M %e, %Y') as dateFormatted
FROM jobs
WHERE
date > DATE_SUB(CURDATE(), INTERVAL 14 DAY)
";
$result = mysql_query($query);

Note: You still have an opportunity to change that 'date' column name ... and 'time' is also reserved ;)

Now, loop through the result set and build the HTML (also note the use of htmlentities):

// No need to fetch each row index as you already extracted them all. 
// Also, if you are going to extract, you may want to consider just
// using mysql_fetch_assoc as opposed to mysql_fetch_array, even
// though PHP won't put the numerical indexes into the global scope
// because they would be invalid variable names.
while ($r = mysql_fetch_assoc [php.net]($result)) {
extract($r);
?>
<span class="style3">
<tr>
<td><font face="Arial" size="2"><?php echo htmlentities [php.net]($job_id);?></font></td>
<td><font face="Arial" size="2"><?php echo htmlentities($client);?></font></td>
<td><font face="Arial" size="2"><?php echo htmlentities(dateFormatted);?></font></td>
<td><font face="Arial" size="2"><?php echo htmlentities($time);?></font></td>
<td><font face="Arial" size="2"><?php echo htmlentities($location);?></font></td>
<td><font face="Arial" size="2"><?php echo htmlentities($machine);?></font></td>
<td><font face="Arial" size="2"><?php echo htmlentities($operator);?></font></td>
<td><a href="view_job.php?id=<?php echo $job_id;?>"><img src="view.jpg"
alt="View Job" width="12" height="16" border="0" /></a></td>
<td><a href="edit_job.php?id=<?php echo $job_id;?>"><img src="edit.png"
alt="Edit Job" border="0" /></a></td>
<td><a href="javascript:confirmDelete('delete_job.php?id=<?php
echo $job_id;?>')"><img src="delete.png" alt="Delete Job"
width="16" height="16" border="0" /></a></td>
<td><a href="print_job.php?id=<?php echo $job_id;?>"><img src="printer.jpg"
alt="Print Job" width="19" height="17" border="0"></a></td>
</tr>
</span>
<?php
}
?>

Now finish off your HTML table, etc.

As far as the result set returning only the 15th of May, you've got me baffled here. Your query says to return only rows where the date is less than two weeks ago. Either your server date is off, or ... did you reformat your DATE data before you altered that column? You may want to run a query against the table and have a look at that column's output -- I bet you forgot to reformat the date data.

brodie_r

11:23 pm on Feb 20, 2006 (gmt 0)

10+ Year Member



Nah i changed it, then delete all entries and re-added them correctly. Output shows fine normally when i echo the date, so i know the format is ok. I shall try what you have suggested tonight after work. Ill keep you posted. Thanks mate.

brodie_r

3:32 pm on Feb 21, 2006 (gmt 0)

10+ Year Member



Code above works, coop just missed the $dateFormatted in the echo ;) Great work mate

coopster

3:52 pm on Feb 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Ah yes, I missed the dollar sign on that variable.

Yes, the code will work, but I truly hope you were more focused on the how it works and a better understanding of PHP retrieving and displaying information from a MySQL database. There is a primer in our PHP Forum Library [webmasterworld.com] that offers some more details on the Basics of extracting data from MySQL using PHP [webmasterworld.com]. It is light reading and might help in better understanding the process.

coopster

4:11 pm on Feb 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I better add to that, it doesn't read as well as I want it to come out ... I read it and it seems like I am coming across harsh, which is not my intention ;)

Scripting languages combined with a database are powerful, and fun too. You can pull and format data in so many ways. The value of understanding how the two can work together and knowing that the database can perform some of the work and formatting becomes a balance. The important thing to remember is that the data coming from the database can often be prepared to look just the way we want it to before it gets handed back to us. Often, but not always. How we retrieve the data is pretty much always the same. We open a database connection handler, execute the query, and process the result set.

I'm happy that you are moving forward with this project and with PHP, brodie_r. If you have any more questions, you know where to find us.

brodie_r

11:25 am on Feb 22, 2006 (gmt 0)

10+ Year Member



Thanks mate, i must thankyou dearly for your excellent help. I must say i had my head around formatting the date using php, but did never realised it could be done in the SQL query. This has proved must successful and a great time saver.

coopster

5:19 pm on Feb 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You are very welcome. And for those times that you will be formatting a date that is not coming from a database table you have already been introduced to PHP's Date and Time Functions -- a bonus ;)

By the way, there is a script in the PHP Forum Library [webmasterworld.com] that you can use to see another of PHP's date manipulation functions. It's in message #6 of Good PHP solutions to small problems [webmasterworld.com]. More tricks of the trade, so to speak. That whole thread is loaded with some goodies.