date() function help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anyday
Forum Newbie
Posts: 8
Joined: Fri Jul 21, 2006 6:06 pm

date() function help

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I would use getdate() for that
AngryPanda
Forum Newbie
Posts: 16
Joined: Wed Jul 19, 2006 12:18 am

Post by AngryPanda »

Code: Select all

echo date('F Y', strtotime('-1 month'));
echo date('F Y');
echo date('F Y', strtotime('+1 month'));
anyday
Forum Newbie
Posts: 8
Joined: Fri Jul 21, 2006 6:06 pm

select with date

Post 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'));
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>
anyday
Forum Newbie
Posts: 8
Joined: Fri Jul 21, 2006 6:06 pm

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
anyday
Forum Newbie
Posts: 8
Joined: Fri Jul 21, 2006 6:06 pm

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I just posted an array of months and weekdays that can be used with this. It is from your other post.
Post Reply