Loop through array to get leap years php - php

I'm a newbie on php and I need help on how to get a loop such as for or foreach to run an array to then define what year in the array is leap or not leap.
$year = '2012,396,300,2000,1100,1089';
$y2 = explode(',',$year);
if (($year % 4 == 0) || ($year % 400 == 0 && $year % 4 != 0)) {
echo "True";
} else{
echo "False";
}
Thank you for your time

Try like this,
// this will check a year is leap year or not and return true/false
function check_leap_year($year){
return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
}
$years = '2012,396,300,2000,1100,1089';
$y2 = explode(',',$years);
foreach($y2 as $year){
$test_leap_year = check_leap_year($year);
if ($test_leap_year) {
echo "True";
} else{
echo "False";
}
}

put conditions inside loop
if((leapyear%4==0 && leapyear%100!==0)||leapyear%400==0)
{
its a leap year
}

Related

If statement not working with between dates

I have the following if statement:
$current=date('d/m/Y');
$next_year=date('y')+1;
$q1a='01/04/'.date('Y');
$q1b='30/06/'.date('Y');
$q2a='01/07/'.date('Y');
$q2b='30/09/'.date('Y');
$q3a='01/09/'.date('Y');
$q3b='31/12/'.date('Y');
$q4a='01/01/20'.$next_year;
$q4b='31/03/20'.$next_year;
if (($current > $q1a) && ($current < $q1b))
{
$currentquarter='Q1';
}
elseif (($current > $q2a) && ($current < $q2b))
{
$currentquarter='Q2';
}
elseif (($current > $q3a) && ($current < $q3b))
{
$currentquarter='Q3';
}
elseif (($current > $q4a) && ($current < $q4b))
{
$currentquarter='Q4';
}
echo $currentquarter;
BUT its returning Q1 even though it should be Q3 as todays date falls between 01/09/2017 and 31/12/2017
You're not comparing dates, you're lexically comparing strings. E.g.:
'21/03/2017' > '01/04/2017'
2 is larger than 0, so this will be true. Character by character the first string sorts "larger than" the second. Again, it's a lexical comparison, not a numeric comparison.
At the very least you need to reverse the endianness to Y/m/d to get the correct result; but you should really construct DateTime objects or UNIX timestamps to do actual date/numeric comparisons properly.
For date comparison in PHP you can use strtotime() function
$date1 = '07/28/2018';
$date2 = '07/28/2017';
if(strtotime($date1) < strtotime($date2))
{
echo 'Date2 is greater then date1';
}
else
{
echo 'Date1 is greater then date2';
}
This would help you simplify the comparison to retrieve the current quarter.
function CurrentQuarter(){
$n = date('n');
if($n < 4){
return "1";
} elseif($n > 3 && $n <7){
return "2";
} elseif($n >6 && $n < 10){
return "3";
} elseif($n >9){
return "4";
}
}
Code Source

checkinf if date is real using php

I'm trying to check if a date is real, it must return true, and if not, it must return false.
It does not seemes to work when I write 35-02-2012 (Date format dd-mm-yy) it return true, but I was expecting false, I do not know where I'm wrong.
below is my function
function isItRealDate($date) {
if ($date == '') {
return false;
} else {
$rxDatePattern = '/^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/'; //Declare Regex
$dtArray = preg_match($rxDatePattern, $date); // is format OK?
if ($dtArray == '0') {
return false;
} else {
$tableau_date = explode('-', $date);
//Checks for dd-mm-yyyy format.
$dtMonth = $tableau_date[1];
$dtDay = $tableau_date[0];
$dtYear = $tableau_date[2];
if ($dtMonth < 1 || $dtMonth > 12) {
return false;
} elseif ($dtDay < 1 || $dtDay > 31) {
return false;
} elseif (($dtMonth == 4 || $dtMonth == 6 || $dtMonth == 9 || $dtMonth == 11) && $dtDay == 31) {
return false;
} elseif ($dtMonth == 2) {
$isleap = ($dtYear % 4 == 0 && ($dtYear % 100 != 0 || $dtYear % 400 == 0));
if ($dtDay > 29 || ($dtDay == 29 && !$isleap)) {
return false;
} else {
return true;
}
}
}
}
}
anykind of help will be much appreciated
If you want something that just works, use checkdate().
As suggested by Ibrahim, use:
function isItRealDate($date) {
if(preg_match("#^(\d{1,2})\-(\d{1,2})\-(\d{1,4})$#", $date, $match)){
//regex could match 99-99-9999 - checkdate will take care.
return checkdate($match[2], $match[1], $match[3]);
}
return false;
}
This will work for ANY (valid) date between 1-1-1 and 31-12-9999

if and else statement mixed up

I am doing the following if else statement below but number (//1) and number (//4) get executed at the same time, I am finding it abit hard to understand why.
<?php
//1
if($a == 1 && count($b) == 0) {
// do this
}
//2
elseif ($a == 1 && count($b) > 0) {
// do that
}
//3
if($a== 0 && count($b) == 0) {
// do a different thing
}
//4
else {
// do the last thing
}
?>
I have done this and it works but i think the should be a more suitable way for not using elseif for this.
else if($a== 0 && count($b) > 0) {
// do the last thing
}
but number (//1) and number (//4) get executed at the same time
It's because you don't have else before the if on //3
//3
if($a== 0 &&
Change to elseif($a== 0 &&
At the moment you have two separate IF conditions
You're missing a closing brace after your first if.
Also, you have a weird operator inside your first condition : $$. Maybe you intended to type &&?
$a = 10;
if ($a == 5) {
echo 'ok';
} elseif ($a == 10) { // $a is equal to 10, so it executes;
echo 'not_ok';
}
if ($a > 20) {
echo 'ok_ok';
} else { // $a is not >20 so else statement executes
echo 'not_not';
}
final result: not_oknot_not
If you are performing such tests on one and the same assignee, but different values, you might not want to execute more than one?
I guess you need elseif where third block is if
if ($a == 5) {
echo 'ok';
} elseif ($a == 10) { // $a is equal to 10, so it executes and stops the block;
echo 'not_ok';
} elseif ($a > 20) {
echo 'ok_ok';
} else { // $a is not >20, but the block was stopped on first elseif
echo 'not_not';
}
produces not_ok
Even if you move the else statement after the first elseif block as was suggested
if ($a > 20) {
echo 'ok_ok';
}
will execute, and if it's true, it will produce result, which again will result in double result
You might want to do this...
if ($a == 1 && count($b) == 0) {
// do this
}
//2
elseif ($a == 1 && count($b) > 0) {
// do that
}
elseif ($a== 0 && count($b) == 0) {
// do a different thing
}
else {
// do the last thing
}
The reason they get executed at the same time is that... Well technically they're not executed at the same time since it's procedural, but they both get executed because they are both different if else conditions. If you want only 1 execution, you should combine them :)

New Error.. Cant Figure This One Out

My timestamp code worked well before i put it in a function.. Get this parse error on line 55, ive marked the line number with "// LINE 55", cant figure it out :(
Here's my error:
Parse error: syntax error, unexpected '.' in C:\wamp\www\flueforumdk\config.php on line 55
Here's the calling of the function:
$GET_UNIX_STAMP_FROM_DB = $art[tidspunkt];
$UNIX_TIME_SECONDS = $GET_UNIX_STAMP_FROM_DB;
echo timestamp_converter($UNIX_TIME_SECONDS);
Here's my function code:
## TIMESTAMP CONVERTER FUNCTION
function timestamp_converter($UNIX_TIME_SECONDS){
// UDREGNING FRA UNIX TIME
$tid = time() - $UNIX_TIME_SECONDS;
$timer = floor($tid/3600);
$minutter = floor($tid/60);
$dage = floor($timer / 24);
$uge = floor($dage / 7);
$month = floor($dage / 30.5);
$aar = floor($dage / 365);
if($tid < 60){
echo"<b>$tid</b> sekunder";
} elseif ($tid > 60){
echo"";
}
if($minutter == 0){
echo"";
} elseif ($minutter < 60){
if($minutter == 1){
echo"<b>$minutter</b> minut";
}else{
echo"<b>$minutter</b> minutter";
}
}
if($timer == 0){
echo"";
} elseif ($timer < 24){
if($timer == 1){
echo"<b>$timer</b> time";
}else{
echo"<b>$timer</b> timer";
}
}
//LINE 55 if($dage == 0){
echo"";
} elseif ($dage < 7){
if($dage == 1){
echo"<b>$dage</b> dag";
}else{
echo"<b>$dage</b> dage";
}
}
if($uge == 0){
echo"";
} elseif ($uge < 4){
if($uge == 1){
echo"<b>$uge</b> uge";
}else{
echo"<b>$uge</b> uger";
}
}
if($month == 0){
echo"";
} elseif ($month < 12){
if($month == 1){
echo"<b>$month</b> måned";
}else{
echo"<b>$month</b> måneder";
}
}
if($aar == 0){
echo"";
} elseif ($aar > 0){
if($aar == 1){
echo"<b>$aar</b> år";
}else{
echo"<b>$aar</b> år";
}
}
}

Unable to increase value in php

I have a function where, if the user enters the correct answer, the score should increment. The validation function is working fine, but the score is not incrementing. Initially, I defined it as 0 and then it should increment based on the answer, but it's not working.
$score =0;
function checkscore ($n, $ans)
{
global $score;
$arr = array('a', 'd');
if (($n == 1) && ($ans == (count(array_intersect($arr, $_POST['a'])) == count($arr))))
{
$score++;
}
if (($n == 2) && ($ans == ($_POST['b'] == 'a')))
{
$score++;
}
if (($n == 3) && ($ans == "div[id='serenade']"))
{
$score++;
}
if (($n == 4) && ($ans != (($_POST['d1'] == 'B') && ($_POST['d2'] == 'C') && ($_POST['d3'] == 'A'))))
{
$score++;
}
return $score;
}
This is the function. Someone please help me.
This function will return 1 if $n is 3 or 4, 0 otherwise. The first two conditions will always be false, while the third will be true when $n==3 and the fourth when $n==4.
So it does increment occasionally, but only up to 1...

Categories