Jump to content

PHP - Days in february


ivan444

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

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();
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.