TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Calendar help (http://www.talkphp.com/advanced-php-programming/4997-calendar-help.html)

Tanax 10-05-2009 11:25 AM

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?

ioan1k 10-05-2009 01:55 PM

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

Tanax 10-05-2009 02:26 PM

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.

ioan1k 10-05-2009 02:33 PM

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 :)

Tanax 10-05-2009 02:58 PM

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?

ETbyrne 10-05-2009 03:17 PM

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'); 


Tanax 11-10-2009 11:20 PM

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.


All times are GMT. The time now is 07:06 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0