Hi
Im having a bit of trouble with my leap_year() function. If I enter in any year it prints “Leap Year” even if I enter in a year that is not a leap year. Please assist
[php]
Hi
Im having a bit of trouble with my leap_year() function. If I enter in any year it prints “Leap Year” even if I enter in a year that is not a leap year. Please assist
[php]
PHP doesn’t know just by entering a year if its a leap year or not. You have to actually create a date and then test that. I did something like that for a resturant. Basically, i was allowing the user to tell the script how to long to run the special for. I allowed them to give a start date, then used a pulldown box to allow them to choose how long to run it for. Below is the first insert code for that part of the project.
[php]
if(isset($_POST[‘addsub’])) {
$name = cleaner(mysql_real_escape_string($_POST[‘name’]));
$desc = cleaner(mysql_real_escape_string($_POST[‘desc’]));
$date = $_POST[‘sdate’];
$tdate = explode(’/’, $_POST[‘sdate’]); //break down user timestamp
$udate = mktime(0,0,0,$tdate[0],$tdate[1],$tdate[2]); //unix timestamp
$price = $_POST[‘price’];
$emp = $_POST[‘emp’];
$runfor = $_POST[‘runfor’];
if($_POST['n_runfor'] != 4) {
$active = 1;
}
switch($_POST['runfor']) {
case '0':
$ndate = strtotime('+1 day', $udate);
break;
case '1':
$ndate = strtotime('+7 days', $udate);
break;
case '2':
$ndate = strtotime('+14 days', $udate);
break;
case '3':
$now = time();
$then = date("t", $now);
$leap = date('L', $now);
if($then == 31) {
$ndate = strtotime('+31 days', $udate);
} elseif($then == 30) {
$ndate = strtotime('+30 days', $udate);
}
if($then == 28 && $leap == 0) { // this is the leap year finder thingy.
$ndate = strtotime('+28 days', $udate);
} else {
$ndate = strtotime('+29 days', $udate);
}
break;
case '4':
$ndate = strtotime('-10 days', $udate);
$active = 0;
break;
}
$ins = mysql_query("INSERT INTO specials VALUES('', '$name', '$desc', '$udate', '$ndate', '$price', '$emp', '$runfor', '$active')") or die(mysql_error());
if($ins) {
$msg = "<div style='color: #3CF; text-align: center; width: 100%;'>$name has been added</div><br />";
}
}
?>[/php]
Maybe it helps you out some?
Don’t worry about cleaner(), its a function i created to sanitize user input.
Thank you for the response. I am new to php and your code is too advanced for me right now, I just need help on my simple code if possible…This is an exercise I’m busy with but I cant seem to get it to work properly
Its really not that advanced if you sit down and look through it. but anyways, use date(‘L’);. it’ll return 1 if it is a leap year, 0 otherwise. So if you’re looking for comparison, it would be something like
[php]
if(date(‘L’, mktime(0, 0, 0, 1, 1, $_POST[‘year’])) { // testing jan 1, whatever the input year is
echo “Yep, its a leap year”;
} else {
echo “Nope, not a leap year”;
}
[/php]
Now i don’t know what your input looks like, so i don’t know if that’ll work or not. Should give you an idea of what to do next though.
but anyways, here is what’s wrong with your code
leap_year();
function leap_year()
yet in the function block you are using $year, $year is not in the global scope, so $year is always undefined (0). and thats why it returns is leap year
leap_year($year);
function leap_year($year)
Great, thanks!