 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
10-05-2009, 11:25 AM
|
#1 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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:
<?phpclass 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?
__________________
|
|
|
|
10-05-2009, 01:55 PM
|
#2 (permalink)
|
|
The Contributor
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
|
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
|
|
|
|
10-05-2009, 02:26 PM
|
#3 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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(3, 2012);
// Gets current month(since we didn't specify a month) information year 2012 $calendar->month(false, 2012);
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.
__________________
|
|
|
|
10-05-2009, 02:33 PM
|
#4 (permalink)
|
|
The Contributor
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
|
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 :)
|
|
|
|
|
The Following User Says Thank You to ioan1k For This Useful Post:
|
|
10-05-2009, 02:58 PM
|
#5 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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?
__________________
|
|
|
|
10-05-2009, 03:17 PM
|
#6 (permalink)
|
|
how quixotic are you?
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
|
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');
|
|
|
|
11-10-2009, 11:20 PM
|
#7 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|