TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-05-2009, 11:25 AM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default Calendar help

Hey!

I'm building a calendar class. It works quite well with getting the current month. Now I want to develop a system/function that makes it possible to change the month or year to future or history.

php Code:
<?php

class Calendar
{

    // The active date, day, month and year
    private $active_date;
    private $active_day;
    private $active_month;
    private $active_year;

    public function __construct()
    {
   
        // Do something?
   
    }
   
    public function month($month = false, $year = false)
    {
   
        $this->set($year, $month);
        return new Month($this->active_month, $this->active_year);
   
    }
   
    private function set($year = false, $month = false)
    {
   
        if($year == false && $month == false)
        {
       
            $this->active_date = time();
            $this->active_day = date('d', $this->active_date);
            $this->active_month = date('m', $this->active_date);
            $this->active_year = date('Y', $this->active_date);
       
        }
   
    }

}

Month is a class that handles the month and calculates the days/weeks/blank days/etc. You don't have to care about that.

As you see I'm calling it via month() where I want to be able to set month and year. If only month is specified it should display the month that was set, with the current year(since that wasn't set). And if only year is set it should display the current month(since month wasn't set) but the year that was set.

If that makes any sense.

Any ideas how to solve that?
__________________
Tanax is offline  
Reply With Quote
Old 10-05-2009, 01:55 PM   #2 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

Very simple to do just add or subtract to the current timestamp

When changing months + or - you just add or subtract
(2678400 * (Number of months))

For a year you would + or minus (31556908 * (Number of Years))

The two numbers
2678400 - Is the number of seconds in a day
31556908 - Number of seconds in a year.

For example a year would be

PHP Code:
// its 2009
$timestamp 1254750653;
echo 
date('Y'$timestamp);
echo 
"\n";
// lets go to say... 2012
$yearPlus $timestamp + (31556908 3);
echo 
date('Y'$yearPlus); 
Month

PHP Code:
// its October
$timestamp 1254750653;
echo 
date('M'$timestamp);
echo 
"\n";
// lets go to say... February
$monthPlus $timestamp + (2678400 4);
// its now February!
echo date('Y'$monthPlus); 
You could of course make the month calculations more accurate by using the number of days in each month for every month you advance ... but the most you will be off is +/- 3 days
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
Old 10-05-2009, 02:26 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

I understand the concept of it, but.. how would I do so I can call it like:
PHP Code:
// Gets current month information
$calendar->month();

// Gets Mars month information(current year since we didn't specify a year)
$calendar->month(3);

// Gets Mars month information year 2012
$calendar->month(32012);

// Gets current month(since we didn't specify a month) information year 2012
$calendar->month(false2012); 
In order for that to work, I would have to get the current timestamp and somehow calculate how many months differs between current month and the month specified(or year specified).. ?

I understand the concept you posted, I just don't know how to.. apply it to what I want.
__________________
Tanax is offline  
Reply With Quote
Old 10-05-2009, 02:33 PM   #4 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

You can use the following function http://us.php.net/date_parse and http://us.php.net/mktime To convert those dates into a timestamp instead of doing the math calculations :)
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
The Following User Says Thank You to ioan1k For This Useful Post:
Tanax (10-05-2009)
Old 10-05-2009, 02:58 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Worked great!

php Code:
private function set($year, $month, $day)
    {
   
        $this->current_date = time();
        $this->current_day = date('d', $this->current_date);
        $this->current_month = date('m', $this->current_date);
        $this->current_year = date('Y', $this->current_date);
   
        // If no month nor year is specified, current month and year will be used
        if($year == false && $month == false)
        {
       
            $this->active_date = $this->current_date;
            $this->active_day = $this->current_day;
            $this->active_month = $this->current_month;
            $this->active_year = $this->current_year;
       
        }
       
        // If month is specified
        elseif($year == false && $month != false)
        {
       
            $this->active_date = mktime(0, 0, 0, $month, $day, $this->current_year);
            $this->active_day = date('d', $this->active_date);
            $this->active_month = date('m', $this->active_date);
            $this->active_year = date('Y', $this->active_date);
       
        }
       
        // If year is specified
        elseif($year != false && $month == false)
        {
       
            $this->active_date = mktime(0, 0, 0, $this->current_month, $day, $year);
            $this->active_day = date('d', $this->active_date);
            $this->active_month = date('m', $this->active_date);
            $this->active_year = date('Y', $this->active_date);
       
        }
       
        // If both year and month is specified
        else
        {
       
            $this->active_date = mktime(0, 0, 0, $month, $day, $year);
            $this->active_day = date('d', $this->active_date);
            $this->active_month = date('m', $this->active_date);
            $this->active_year = date('Y', $this->active_date);
       
        }
   
    }

How would you suggest I do the "navigation" of this calendar? So people can press like "next month", "next year", "last month", "last year", etc.

Should I do that with GET?
__________________
Tanax is offline  
Reply With Quote
Old 10-05-2009, 03:17 PM   #6 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

Another solution would be to use PHP's DateTime class to manipulate dates.

PHP Code:
$date = new Datetime('Aug 2009');
$date->modify('+1 years');
echo 
$date->format('M Y'); 
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 11-10-2009, 11:20 PM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Mhm, that would work aswell, though I used the first one.

Anyhow.
I have another problem here. I created this class, and right now Sunday is the beginning of the week when I output the calendar. How would I change it so it's Monday instead?

Edit: Nvm. Solved it.
__________________

Last edited by Tanax : 11-15-2009 at 05:18 PM.
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding Event to PHP Calendar buildakicker General 21 01-29-2013 12:01 PM
Coloring today and week-end in a calendar... Peuplarchie Advanced PHP Programming 5 12-05-2008 02:22 PM
Calendar Script - Week View oMIKEo Absolute Beginners 2 09-05-2008 09:11 PM
Calendar script needs template system nonenone General 3 04-16-2005 04:15 AM


All times are GMT. The time now is 11:08 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design