Page 1 of 1
date() function help
Posted: Fri Jul 21, 2006 6:09 pm
by anyday
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
Posted: Fri Jul 21, 2006 6:12 pm
by Luke
I would use getdate() for that
Posted: Fri Jul 21, 2006 6:28 pm
by AngryPanda
Code: Select all
echo date('F Y', strtotime('-1 month'));
echo date('F Y');
echo date('F Y', strtotime('+1 month'));
select with date
Posted: Thu Jul 27, 2006 2:34 pm
by anyday
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.
Code: Select all
$month1=date('m');
$result = mysql_query("select * from leads WHERE month=$month1 ORDER BY lname") or
die (mysql_error());
using the code below, anyone know an easy way for when the user pics the previous month have the month var change?
Code: Select all
<?php
echo date('F Y', strtotime('-1 month'));
echo date('F Y');
echo date('F Y', strtotime('+1 month'));
?>
Posted: Thu Jul 27, 2006 2:38 pm
by pickle
In the manual for
date() it says the 2nd argument can be a timestamp. Call
Posted: Thu Jul 27, 2006 3:06 pm
by RobertGonzalez
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>
Posted: Thu Jul 27, 2006 3:11 pm
by anyday
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?
Posted: Thu Jul 27, 2006 3:17 pm
by RobertGonzalez
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...
Code: Select all
<a href="page.php?month=5">Previous Month</a>
... 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.
Posted: Thu Jul 27, 2006 3:25 pm
by anyday
thanks alot for the help, it works fine now.
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
Posted: Fri Jul 28, 2006 12:52 am
by RobertGonzalez
I just posted an array of months and weekdays that can be used with this. It is from your other post.