Jump to content

Problem with Dates (months)


anser316

Recommended Posts

I have a problem with showing rows of data from MYSQL to pHP that have the date of the current month, previous month, current year and previous year. i want to show on the following php forms.

i just would like to know how to match the month or year, or get previous month or year, functions

E.G.

MYSQL

ID  DATE

01  2008-3-12

02  2008-3-10

03  2008-2-10

04  2008-2-9

05  2007-11-9

06  2007-02-8

 

i want to show on the following php forms

CURRENT MONTH FORM          PREVIOUS MONTH FORM  CURRENT YEAR FORM  PREVIOUS YEAR FORM

01  2008-3-12                    03  2008-2-10              01  2008-3-12          06  2007-02-8

02  2008-3-10                    04  2008-2-9                02  2008-3-10

                                                                            03  2008-2-10

                                                                            04  2008-2-9

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/97621-problem-with-dates-months/
Share on other sites

this is the code i have so far, after i connect to the mysql

 

$result =mysql_query("SELECT id,date
FROM date");

echo "<br><br>";
echo "<B>Current Month</B><p>";
echo "<table border='1'>";
echo "<tr> <th>ID</th> <th>Date</th></tr>";
while($row = mysql_fetch_array( $result )) {

echo "<tr><td>"; 
echo $row['id'];
echo "</td><td>";
echo $row['date'];
echo "</td></tr>"; 

}
echo "</table>";

 

i can now display data from current month and year, but still not previous

i tried using where month(date) =month(Current_date()-1), but that does not work

$result =mysql_query("SELECT id,date
FROM date
where month(date) =month(Current_date())");

echo "<br><br>";
echo "<B>Current Month</B><p>";
echo "<table border='1'>";
echo "<tr> <th>ID</th> <th>Date</th></tr>";
while($row = mysql_fetch_array( $result )) {

echo "<tr><td>"; 
echo $row['id'];
echo "</td><td>";
echo $row['date'];
echo "</td></tr>"; 

}
echo "</table>";

I HAVE NOW SOLVED IT BY USING:

 

CURRENT MONTH:

WHERE MONTH(presc_date) = MONTH(DATE_ADD(CURDATE(),INTERVAL 0 MONTH))

 

PREVIOUS MONTH:

WHERE MONTH(presc_date) = MONTH(DATE_ADD(CURDATE(),INTERVAL -1 MONTH))

 

CURRENT YEAR:

WHERE YEAR(presc_date) = YEAR(DATE_ADD(CURDATE(),INTERVAL 0 YEAR))

 

PREVIOUS YEAR:

WHERE YEAR(presc_date) = YEAR(DATE_ADD(CURDATE(),INTERVAL -1 YEAR))

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.