Jump to content

Recommended Posts

I need this logic in Class not just the function

This is the drop down list

 

$cutoff = 1900;

    $now = date('Y');

 

    // YEARS

    echo '<select name="year">' . PHP_EOL;

    for ($y=$now; $y>=$cutoff; $y--) {

        echo '  <option value="' . $y . '">' . $y . '</option>' . PHP_EOL;

    }

    echo '</select>' . PHP_EOL;

 

 

Now i need to get number of days in february by clicking

Only class varianty... please...tnx

Edited by ivan444

Is this what you're wanting?

class february
{
    private $year;
    
    public function __construct($year)
    {
        $this->year = $year;
    }
    
    public function numDays()
    {
        $d = new DateTime("{$this->year}-02-01");
        return $d->format('t');
    }
}

$f = new february(1900);
echo $f->numdays();  // 28

$f = new february(2000);
echo $f->numdays();  // 29

I really don't know.It just wont work.

The thing is i need to make 2 node++ files. One is index.php where is the drop down with years from 1900 to 2015 and where i call the class february.

And the second script is class.php where is the class...Here is the code in those scripts...please reply if you can fix my problem.

 

                                                    class.php

<?php

include("index.php");
class february
{
 
    private $year;
    
    public function __construct($year)
    {
        $this->year = $year;
    }
    
    public function numDays()
    {
        $d = new DateTime("{$this->year}-02-01");
        return $d->format('t');
    }
}
 
 
?>
 
                                         index.php
<?php
include("class.php");
 
 
    $cutoff = date(1900);
    $now = date('Y');
 
    echo '<select name="year">' . PHP_EOL;
    for ($y=$now; $y>=$cutoff; $y--) {
        echo '  <option value="' . $y . '">' . $y . '</option>' . PHP_EOL;
    }
    echo '</select>' . PHP_EOL;
 
$f = new february((int)$_POST['year']);
    echo $f->numdays();
?>
Edited by ivan444

The select needs to be inside <form> tags (hence my previous link to php forms). When the form is submitted the contents are passed in the $_GET or $_POST arrays depending on the form method. (Use POST when sending data to perform an update and GET when you just want to get information)

include("class.php");

if (isset($_GET['year'])) {
    $f = new february((int)$_GET['year']);
    $days = $f->numdays();
    echo "There were $days days in February {$_GET['year']}<hr>";
} 
$cutoff = date(1900);
$now = date('Y');
?>
<html>
<body>
    <form method='get' action=''>
        <?php
        echo '<select name="year">' . PHP_EOL;
        for ($y=$now; $y>=$cutoff; $y--) {
            echo '  <option value="' . $y . '">' . $y . '</option>' . PHP_EOL;
        }
        echo '</select>' . PHP_EOL;
        ?>
    
        <input type='submit' name='btnSubmit' value='Submit'>
    </form>

</body>
</html> 
<?php

include("class.php");

 

if (isset($_GET['year'])) {

    $f = new february((int)$_GET['year']);

    $days = $f->numdays();

    echo "There were $days days in February {$_GET['year']}<hr>";


$cutoff = date(1900);

$now = date('Y');

?>

<html>

<body>

    <form method='get' action=''>

        <?php

        echo '<select name="year">' . PHP_EOL;

        for ($y=$now; $y>=$cutoff; $y--) {

            echo '  <option value="' . $y . '">' . $y . '</option>' . PHP_EOL;

        }

        echo '</select>' . PHP_EOL;

        ?>

    

        

    </form>

 

</body>

</html> 

I still got the error

Fatal error: Maximum execution time of 30 seconds exceeded in H:\xampp\htdocs\NOVO\class.php on line 2

 

Can you copy paste what you have please

 

here is the class.php

 

<?php
include("index.php");
class february
{
 
    private $year;
    
    public function __construct($year)
    {
        $this->year = $year;
    }
    
    public function numDays()
    {
        $d = new DateTime("{$this->year}-02-01");
        return $d->format('t');
    }
}
 
 
?>
Edited by ivan444
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.