date() function help
Moderator: General Moderators
date() function help
im trying to figure out how to use the date() function to step between the months on a website of mine.
what i have is a website that shows my production with my company. based on what month it is i have a mysql SELECT statement setup to only select the current months production records.
<p align="center" class="style1"><? echo date('F')." ".date('Y') ?> Totals</p>
im trying to add a link to either side of this label for previous month and next month. not sure how to go about this though ?
thanks
what i have is a website that shows my production with my company. based on what month it is i have a mysql SELECT statement setup to only select the current months production records.
<p align="center" class="style1"><? echo date('F')." ".date('Y') ?> Totals</p>
im trying to add a link to either side of this label for previous month and next month. not sure how to go about this though ?
thanks
-
AngryPanda
- Forum Newbie
- Posts: 16
- Joined: Wed Jul 19, 2006 12:18 am
Code: Select all
echo date('F Y', strtotime('-1 month'));
echo date('F Y');
echo date('F Y', strtotime('+1 month'));select with date
thanks for the reply that last example works good. The problem now is the following:
view.php of my site is set to do a select statement based on the current month and only pull records from that month.
using the code below, anyone know an easy way for when the user pics the previous month have the month var change?
view.php of my site is set to do a select statement based on the current month and only pull records from that month.
Code: Select all
$month1=date('m');
$result = mysql_query("select * from leads WHERE month=$month1 ORDER BY lname") or
die (mysql_error());Code: Select all
<?php
echo date('F Y', strtotime('-1 month'));
echo date('F Y');
echo date('F Y', strtotime('+1 month'));
?>In the manual for date() it says the 2nd argument can be a timestamp. Call
Code: Select all
date('m',strtotime('-1 month'));Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Code: Select all
<?php
$this_month = date('m');
if (isset($_GET['month']) && isnumeric($_GET['month']))
{
if ($_GET['month'] <=12 && $_GET['month'] >= 01)
{
$this_month = $_GET['month'];
}
}
$last_month = ( $this_month == 01) ? 12 : $this_month - 1;
$next_month = ( $this_month == 12) ? 1 : $this_month + 1;
?>
<a href="?month=<?php echo $last_month; ?>">Last Month</a> :: <a href="?month=<?php echo $next_month; ?>">Next Month</a>everah thanks that looks good, but the only problem im having now is the actuall links to the different months and getting the SELECT statement to match. what ide like to do is make a Previous Month link that points to the same view.php page but with a _POST maybe like <a href="view.php?month=06">Previous Month</a> and have the database query on that given POST. problem with that is if they go back one month then try to go back another month would it work since the month is based off of the current month?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
The code I posted sets the month based on the current month as a default. Then it looks to see if the month var of the query string is set...
... and if it is set, it changes this month to the month that was passed to it. After that it automatically adjusts last month and next month based on the this month.
PS, POST is not the correct term to use for vars passed by a URL. They are GET. POST are those sent by a form. Just an FYI.
Code: Select all
<a href="page.php?month=5">Previous Month</a>PS, POST is not the correct term to use for vars passed by a URL. They are GET. POST are those sent by a form. Just an FYI.
thanks alot for the help, it works fine now.
Here is my link code
Also the $this_month var , is there anyway to have that converted into a English type print like 7=July 2006? as u can see in my code at the top of page the last month and next month are inbetween a echo fo the current month and year in english. but the way it is put in there every time the user changes month's that text would always be the same
Here is my link code
Code: Select all
<p align="center" class="style1"><a href="view.php?month=<?php echo $last_month; ?>">Last Month</a> :: <? echo date('F')." ".date('Y') ?> Totals :: <a href="view.php?month=<?php echo $next_month; ?>">Next Month</a></a></p>Also the $this_month var , is there anyway to have that converted into a English type print like 7=July 2006? as u can see in my code at the top of page the last month and next month are inbetween a echo fo the current month and year in english. but the way it is put in there every time the user changes month's that text would always be the same
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I just posted an array of months and weekdays that can be used with this. It is from your other post.