ivan444 Posted July 4, 2015 Share Posted July 4, 2015 Hello guys I really need some help please I need to create a class, for example "yearCheckFeb" I have a drop down list with years from 1900 to 2015 and when you choose a year you must get number of days in february. Can someone help my with this,please Thanks Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted July 4, 2015 Share Posted July 4, 2015 See documentation on cal_days_in_month Alternatively pass a timestamp to date and use t as the format Quote Link to comment Share on other sites More sharing options...
ivan444 Posted July 4, 2015 Author Share Posted July 4, 2015 (edited) 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 July 4, 2015 by ivan444 Quote Link to comment Share on other sites More sharing options...
Barand Posted July 5, 2015 Share Posted July 5, 2015 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 Quote Link to comment Share on other sites More sharing options...
ivan444 Posted July 6, 2015 Author Share Posted July 6, 2015 Yeah,but i need this code to work with a drop down list. When you click a year from 1900 to 2015 you should get number of days in february ?! Quote Link to comment Share on other sites More sharing options...
Barand Posted July 6, 2015 Share Posted July 6, 2015 http://uk1.php.net/manual/en/tutorial.forms.php Quote Link to comment Share on other sites More sharing options...
ivan444 Posted July 6, 2015 Author Share Posted July 6, 2015 (edited) 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 July 6, 2015 by ivan444 Quote Link to comment Share on other sites More sharing options...
Barand Posted July 6, 2015 Share Posted July 6, 2015 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> Quote Link to comment Share on other sites More sharing options...
ivan444 Posted July 6, 2015 Author Share Posted July 6, 2015 i copied your solution in my index.php script but i got this error Fatal error: Maximum execution time of 30 seconds exceeded in H:\xampp\htdocs\PHP1\index.php on line 2 Quote Link to comment Share on other sites More sharing options...
Barand Posted July 6, 2015 Share Posted July 6, 2015 Works fine for me. Post the whole code from your "index.php" Quote Link to comment Share on other sites More sharing options...
ivan444 Posted July 6, 2015 Author Share Posted July 6, 2015 <?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> Quote Link to comment Share on other sites More sharing options...
Barand Posted July 6, 2015 Share Posted July 6, 2015 Your form is missing the "Submit" button. When I added that there were no problems. I did not get your timeout error. Quote Link to comment Share on other sites More sharing options...
ivan444 Posted July 6, 2015 Author Share Posted July 6, 2015 (edited) 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 July 6, 2015 by ivan444 Quote Link to comment Share on other sites More sharing options...
ivan444 Posted July 6, 2015 Author Share Posted July 6, 2015 It works i just removed the include("index.php"); from class.php Thank you Barand Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.