Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7032353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:55:27+00:00 2026-05-28T00:55:27+00:00

I’m trying to create a calendar class in php and am having issues returning

  • 0

I’m trying to create a calendar class in php and am having issues returning class variables which are defined as public.

Here is the code for my class

<?php 

class CalendarClass {

// property declaration

public $months = array();
public $year;
public $month;
public $day;
public $fourYear;
public $monthFirst;
public $monthMax;
public $monthVal;

private $data = array();

//My set functions

function __construct() {
    $year = date("y");;
    $month = date("n");;
    $day = date("j");;
    $fourYear = date("Y");
    $this->setDate($year, $month, $day, $fourYear);
    echo $fourYear; // echos 2012
}

//I'm not entirely sure if these 2 are set up correctly
//From what I read these are automatically called when
//you set or get variables.
public function __set($dt, $vl) {
    $this->data[$dt] = $vl;
}

public function __get($dt) {
    return $this->data[$dt];
}


//Sets the date and all other variables accordingly
//Must have in this order
//2 digit year, 1/2 digit month and day (ex: 1, 12), 4 digit year 
public function setDate($y, $m, $d, $fourY){
    $year = $y;
    $month = $m;
    $day = $d;
    $fourYear = $fourY; 

    if($day == -1){
        $month = $month - 1;
        if($month == -1){
            $month = 11;
            $fourYear--;
            $year = substr($fourYear, -2);
        }
    }elseif($day > $this->monthMax){
        $month = $month + 1;
        if($month == 12){
            $month = 11;
            $fourYear++;
            $year = substr($fourYear, -2);
        }
    }

    if($month == -1){
        $month = 11;
        $fourYear--;
        $year = substr($fourYear, -2);
    }elseif($month == 12){
        $month = 11;
        $fourYear++;
        $year = substr($fourYear, -2);
    }

    $monthFirst = $this->valueSet($year, $month, $day, $fourYear);
    $monthMax = $this->monthReturn($month, "Max");
    $monthVal = $this->monthReturn($month, "Value");
}

//Get functions
public function getYear(){
    $test = $this->year;
    echo $test; //echos nothing
    return $this->year; 
}
public function getMonth(){
    return $this->month;    
}
public function getDay(){
    return $this->day;  
}
public function getFourYear(){
    return $this->fourYear; 
}
public function getMonthFirst(){
    return $this->monthFirst;   
}
public function getMonthMax(){
    return $this->monthMax; 
}
public function getMonthVal(){
    return $this->monthVal; 
}

// method declaration

//Returns the year portion of the equation
public function year($currentYear){

    //calculating year value
    $yearTotal = 0;
    if($currentYear > 84){
        $yearTotal = $currentYear - 84;
    }elseif($currentYear > 56){
        $yearTotal = $currentYear - 56;
    }elseif($currentYear > 28){
        $yearTotal = $currentYear - 28;
    }else{
        $yearTotal = $currentYear;
    }
    $yearTotal = $yearTotal + floor($currentYear / 4);
    while($yearTotal > 7){
        $yearTotal = $yearTotal - 7;
    }
    return $yearTotal;
}

//Returns the month portion of the equation
public function month($currentMonth, $four_Year){
    //Calculating month value

    //$this->month;
    $monthTotal = $this->monthReturn($currentMonth, "Value");
    if((($four_Year % 4) == 0) && ($currentMonth == 0 || $currentMonth == 1)){

            $monthTotal = $monthTotal - 1;
    }
    return $monthTotal;
}

//Returns the day portion of equation
public function day($currentDay){

    //Calculating day value
    $dayTotal = $currentDay;
    while($dayTotal > 7){
        $dayTotal = $dayTotal - 7;  
    }
    return $dayTotal;
}

//Return the total value for equation(weekday 
//"0" for sunday 
//"1" for monday 
//"2" for tuesday
public function total($yearTotal, $monthTotal, $dayTotal){

    $total = $yearTotal + $monthTotal + $dayTotal;

    $fourYear = date('Y');

    if($fourYear > 1699 && $fourYear < 1800){
        $total = $total + 5;
    }
    if($fourYear > 1799 && $fourYear < 1900){
        $total = $total + 3;    
    }
    if($fourYear > 1899 && $fourYear < 2000){
        $total = $total + 1;
    }
    if($fourYear > 1999 && $fourYear < 2100){
        $total = $total + 0;
    }
    if($fourYear > 2099 && $fourYear < 2200){
        $total = $total + 2;
    }
    if($fourYear > 2099 && $fourYear < 2200){
        $total = $total + 4;
    }


    while($total > 7){
        $total = $total - 7;
    }
    return $total;
}   

//Return the Maximum amount of day in the month
//or return the month value for equation
//2nd variable should be either 
//"Max" - for maximum amount of days in month or 
//"Value" - for the month value
public function monthReturn($month, $action){
    global $months;
    $m = $month - 1;
    if($action == "Max"){
        $max = $months[$m][0];
        return $max;
    }elseif($action == "Value"){
        $value = $months[$m][1];
        return $value;
    }else{
        return -1;  
    }

}

//Get the weekday of a certain day
public function valueSet($currentYear, $currentMonth, $currentDay, $four_Year){

$y = $this->year($currentYear);
//echo $y . "<br />";
$m = $this->month($currentMonth, $four_Year);
//echo $m . "<br />";
$d = $this->day($currentDay);
//echo $d . "<br />";
$weekday = $this->total($y, $m, $d);
return $weekday;

}
    //End of class  
    }



    ?>

And here is the code using that class

    include_once("f_calendar.php"); 

    $thisMonth = new CalendarClass;

    $nextMonth = new CalendarClass;

    $prevMonth = new CalendarClass;

    $y = $thisMonth->getYear();

    $m = $thisMonth->getMonth();

    $d = $thisMonth->getDay();

    $fY = $thisMonth->getFourYear();

    $thisMonth->setDate($y, $m, $d, $fY);

    $nextMonth->setDate($y, $m + 1, "1", $fY);

    $prevMonth->setDate($y, $m - 1, "1", $fY);

    echo $thisMonth->fourYear; //echoes nothing

    $weekDay = $thisMonth->getMonthFirst();

    $testYear = $thisMonth->getYear();

    $i = $prevMonth->getMonthMax() - $weekDay;

    $totalDays = $weekDay + $thisMonth->getMonthMax();

    $totalWeeks = ceil($totalDays / 7);

    $days = $totalWeeks * 7;

    $prevMonthMax = $prevMonth->getMonthMax();

    $thisMonthMax = $thisMonth->getMonthMax();

    $nextMonthMax = $nextMonth->getMonthMax();

    ?>

The objects are being defined because flag echoes are output to the screen from within them. I originally thought it would work with my own get functions but when that didn’t work I started looking more into it and learned of the magic functions __set, __get but I have not followed through with those yet. Do I have to redo most of my code to accommodate for the magic functions? or is there a simpler and easier method to go about it? Particularly with my current code and without I intend to reuse this code so I would like to make it 100% correct so I never have to redo it.

Thanks
Andrew

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:55:28+00:00Added an answer on May 28, 2026 at 12:55 am

    Your problem is, that you never assign a value to the object attribute “fourYear” of the class.
    You do it like this

    $fourYear = 'new value';
    

    You have to do it like this:

    $this->fourYear = 'new value';
    

    Please don’t use the magic functions __get and __set. You don’t need them here.
    You need them, if you want to do something with the value before you store it.

    Here is your updatet and working class:

    <?php 
    
    class CalendarClass {
    
    // property declaration
    
    public $months = array();
    public $year;
    public $month;
    public $day;
    public $fourYear;
    public $monthFirst;
    public $monthMax;
    public $monthVal;
    
    private $data = array();
    //My set functions
    
    function __construct() {
        $year = date("y");;
        $month = date("n");;
        $day = date("j");;
        $this->fourYear = date("Y");
        $this->setDate($year, $month, $day, $this->fourYear);
        echo $this->fourYear; // echos 2012
    }
    
    //I'm not entirely sure if these 2 are set up correctly
    //From what I read these are automatically called when
    //you set or get variables.
    
    
    //Sets the date and all other variables accordingly
    //Must have in this order
    //2 digit year, 1/2 digit month and day (ex: 1, 12), 4 digit year 
    public function setDate($y, $m, $d, $fourY){
        $year = $y;
        $month = $m;
        $day = $d;
        $this->fourYear = $fourY; 
    
        if($day == -1){
            $month = $month - 1;
            if($month == -1){
                $month = 11;
                $this->fourYear--;
                $year = substr($this->fourYear, -2);
            }
        }elseif($day > $this->monthMax){
            $month = $month + 1;
            if($month == 12){
                $month = 11;
                $this->fourYear++;
                $year = substr($this->fourYear, -2);
            }
        }
    
        if($month == -1){
            $month = 11;
            $this->fourYear--;
            $year = substr($this->fourYear, -2);
        }elseif($month == 12){
            $month = 11;
            $this->fourYear++;
            $year = substr($this->fourYear, -2);
        }
    
        $monthFirst = $this->valueSet($year, $month, $day, $this->fourYear);
        $monthMax = $this->monthReturn($month, "Max");
        $monthVal = $this->monthReturn($month, "Value");
    }
    
    //Get functions
    public function getYear(){
        $test = $this->year;
        echo $test; //echos nothing
        return $this->year; 
    }
    public function getMonth(){
        return $this->month;    
    }
    public function getDay(){
        return $this->day;  
    }
    public function getFourYear(){
        return $this->fourYear; 
    }
    public function getMonthFirst(){
        return $this->monthFirst;   
    }
    public function getMonthMax(){
        return $this->monthMax; 
    }
    public function getMonthVal(){
        return $this->monthVal; 
    }
    
    // method declaration
    
    //Returns the year portion of the equation
    public function year($currentYear){
    
        //calculating year value
        $yearTotal = 0;
        if($currentYear > 84){
            $yearTotal = $currentYear - 84;
        }elseif($currentYear > 56){
            $yearTotal = $currentYear - 56;
        }elseif($currentYear > 28){
            $yearTotal = $currentYear - 28;
        }else{
            $yearTotal = $currentYear;
        }
        $yearTotal = $yearTotal + floor($currentYear / 4);
        while($yearTotal > 7){
            $yearTotal = $yearTotal - 7;
        }
        return $yearTotal;
    }
    
    //Returns the month portion of the equation
    public function month($currentMonth, $four_Year){
        //Calculating month value
    
        //$this->month;
        $monthTotal = $this->monthReturn($currentMonth, "Value");
        if((($four_Year % 4) == 0) && ($currentMonth == 0 || $currentMonth == 1)){
    
                $monthTotal = $monthTotal - 1;
        }
        return $monthTotal;
    }
    
    //Returns the day portion of equation
    public function day($currentDay){
    
        //Calculating day value
        $dayTotal = $currentDay;
        while($dayTotal > 7){
            $dayTotal = $dayTotal - 7;  
        }
        return $dayTotal;
    }
    
    //Return the total value for equation(weekday 
    //"0" for sunday 
    //"1" for monday 
    //"2" for tuesday
    public function total($yearTotal, $monthTotal, $dayTotal){
    
        $total = $yearTotal + $monthTotal + $dayTotal;
    
        $this->fourYear = date('Y');
    
        if($this->fourYear > 1699 && $this->fourYear < 1800){
            $total = $total + 5;
        }
        if($this->fourYear > 1799 && $this->fourYear < 1900){
            $total = $total + 3;    
        }
        if($this->fourYear > 1899 && $this->fourYear < 2000){
            $total = $total + 1;
        }
        if($this->fourYear > 1999 && $this->fourYear < 2100){
            $total = $total + 0;
        }
        if($this->fourYear > 2099 && $this->fourYear < 2200){
            $total = $total + 2;
        }
        if($this->fourYear > 2099 && $this->fourYear < 2200){
            $total = $total + 4;
        }
    
    
        while($total > 7){
            $total = $total - 7;
        }
        return $total;
    }   
    
    //Return the Maximum amount of day in the month
    //or return the month value for equation
    //2nd variable should be either 
    //"Max" - for maximum amount of days in month or 
    //"Value" - for the month value
    public function monthReturn($month, $action){
        global $months;
        $m = $month - 1;
        if($action == "Max"){
            $max = $months[$m][0];
            return $max;
        }elseif($action == "Value"){
            $value = $months[$m][1];
            return $value;
        }else{
            return -1;  
        }
    
    }
    
    //Get the weekday of a certain day
    public function valueSet($currentYear, $currentMonth, $currentDay, $four_Year){
    
    $y = $this->year($currentYear);
    //echo $y . "<br />";
    $m = $this->month($currentMonth, $four_Year);
    //echo $m . "<br />";
    $d = $this->day($currentDay);
    //echo $d . "<br />";
    $weekday = $this->total($y, $m, $d);
    return $weekday;
    
    }
        //End of class  
        }
    
    
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.