$dt=date('d/m/Y'); // Gives Todays date
$day = date('w'); // Gives week day number sun-sat is 0-6, sunday is zero
$sday = $day - 0; // Last sunday
$eday = 7 - $day; // Next sunday
$ws = date('d/m/Y', strtotime('-'.$sday.' days'));
$lastsunday = date('d/m/Y', strtotime('-'.$sday.' days'));
$we = date('d/m/Y', strtotime('+'.$eday.' days'));
$nextsunday = date('d/m/Y', strtotime('+'.$eday.' days'));
$no_of_mand=16;
$chkrep = mysql_result(mysql_query("SELECT IFNULL(count(*),0) FROM meerepdone where
STR_TO_DATE(tdate,'%d/%m/%Y')>=STR_TO_DATE('$ws','%d/%m/%Y') and
STR_TO_DATE(tdate,'%d/%m/%Y')<=STR_TO_DATE('$we','%d/%m/%Y')"),0);
if($chkrep==0) // The week records exists in DB or not
{
echo "<br/><br/><center><font color='darkblue'><h2>There are no Records in this week in DB</h2></font>
</center>";
echo "<br/><br/><center><font color='darkblue'><h2> FROM DATE : $ws - To date : $we</h2></font>
</center>";
exit(0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
/* $query="SELECT distinct tdate from meerep where
STR_TO_DATE(tdate,'%d/%m/%Y')>=STR_TO_DATE('$ws','%d/%m/%Y') and
STR_TO_DATE(tdate,'%d/%m/%Y')<=STR_TO_DATE('$we','%d/%m/%Y')"; */
$initqry=mysql_result(mysql_query("SELECT IFNULL(count(*),0) FROM fortnight where
STR_TO_DATE(fromdt,'%d/%m/%Y')=STR_TO_DATE('$ws','%d/%m/%Y') and
STR_TO_DATE(todate,'%d/%m/%Y')=STR_TO_DATE('$we','%d/%m/%Y')"),0);
/* the above Query compares the php dates with mysql dates */
if($initqry==0){
$transqry = "SELECT DISTINCT mandal from meerep";
$res=mysql_query($transqry);
$nor=mysql_numrows($res);
$i=0;$mand='';
$iconn = new mysqli($servername, $username, $password, $database);
while($i<$nor){
$mand=mysql_result($res,$i,"mandal");
$insqry = "INSERT INTO fortnight(mandal,fromdt,todate) VALUES('$mand','$lastsunday','$nextsunday')";
$iconn->query($insqry);
$i++;
}
$iconn->close();
$query = "SELECT DISTINCT mandal from meerep";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
$sl=1;$mand='';
$getpendper = 0;
$weekper = 0;
$iconn = new mysqli($servername, $username, $password, $database);
while($i<$num){
$mand=mysql_result($result,$i,"mandal");
$minper = mysql_result(mysql_query("SELECT IFNULL(MIN(pendper),0) FROM meerep where
STR_TO_DATE(tdate,'%d/%m/%Y')>=STR_TO_DATE('$lastsunday','%d/%m/%Y') and
STR_TO_DATE(tdate,'%d/%m/%Y')<=STR_TO_DATE('$nextsunday','%d/%m/%Y') and mand=trim('$mand')"),0);
$maxper = mysql_result(mysql_query("SELECT IFNULL(MAX(pendper),0) FROM meerep where
STR_TO_DATE(tdate,'%d/%m/%Y')>=STR_TO_DATE('$lastsunday','%d/%m/%Y') and
STR_TO_DATE(tdate,'%d/%m/%Y')<=STR_TO_DATE('$nextsunday','%d/%m/%Y') and mand=trim('$mand')"),0);
$weekper = 100 - round(($minper/$maxper) * 100) ; // calculating percentage
$updqry = "UPDATE fortnight SET percen='$weekper' WHERE mandal='$mand' and fromdt='$lastsunday' and todate='$nextsunday'";
$iconn->query($updqry); /* A simple Update through PHP */
$i++; $sl++;
} // End of While loop
$iconn->close();
} // End of If condition fortnight having zero records for the week dates
$day = date('w'); // Gives week day number sun-sat is 0-6, sunday is zero
$sday = $day - 0; // Last sunday
$eday = 7 - $day; // Next sunday
$ws = date('d/m/Y', strtotime('-'.$sday.' days'));
$lastsunday = date('d/m/Y', strtotime('-'.$sday.' days'));
$we = date('d/m/Y', strtotime('+'.$eday.' days'));
$nextsunday = date('d/m/Y', strtotime('+'.$eday.' days'));
$no_of_mand=16;
$chkrep = mysql_result(mysql_query("SELECT IFNULL(count(*),0) FROM meerepdone where
STR_TO_DATE(tdate,'%d/%m/%Y')>=STR_TO_DATE('$ws','%d/%m/%Y') and
STR_TO_DATE(tdate,'%d/%m/%Y')<=STR_TO_DATE('$we','%d/%m/%Y')"),0);
if($chkrep==0) // The week records exists in DB or not
{
echo "<br/><br/><center><font color='darkblue'><h2>There are no Records in this week in DB</h2></font>
</center>";
echo "<br/><br/><center><font color='darkblue'><h2> FROM DATE : $ws - To date : $we</h2></font>
</center>";
exit(0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
/* $query="SELECT distinct tdate from meerep where
STR_TO_DATE(tdate,'%d/%m/%Y')>=STR_TO_DATE('$ws','%d/%m/%Y') and
STR_TO_DATE(tdate,'%d/%m/%Y')<=STR_TO_DATE('$we','%d/%m/%Y')"; */
$initqry=mysql_result(mysql_query("SELECT IFNULL(count(*),0) FROM fortnight where
STR_TO_DATE(fromdt,'%d/%m/%Y')=STR_TO_DATE('$ws','%d/%m/%Y') and
STR_TO_DATE(todate,'%d/%m/%Y')=STR_TO_DATE('$we','%d/%m/%Y')"),0);
/* the above Query compares the php dates with mysql dates */
if($initqry==0){
$transqry = "SELECT DISTINCT mandal from meerep";
$res=mysql_query($transqry);
$nor=mysql_numrows($res);
$i=0;$mand='';
$iconn = new mysqli($servername, $username, $password, $database);
while($i<$nor){
$mand=mysql_result($res,$i,"mandal");
$insqry = "INSERT INTO fortnight(mandal,fromdt,todate) VALUES('$mand','$lastsunday','$nextsunday')";
$iconn->query($insqry);
$i++;
}
$iconn->close();
$query = "SELECT DISTINCT mandal from meerep";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
$sl=1;$mand='';
$getpendper = 0;
$weekper = 0;
$iconn = new mysqli($servername, $username, $password, $database);
while($i<$num){
$mand=mysql_result($result,$i,"mandal");
$minper = mysql_result(mysql_query("SELECT IFNULL(MIN(pendper),0) FROM meerep where
STR_TO_DATE(tdate,'%d/%m/%Y')>=STR_TO_DATE('$lastsunday','%d/%m/%Y') and
STR_TO_DATE(tdate,'%d/%m/%Y')<=STR_TO_DATE('$nextsunday','%d/%m/%Y') and mand=trim('$mand')"),0);
$maxper = mysql_result(mysql_query("SELECT IFNULL(MAX(pendper),0) FROM meerep where
STR_TO_DATE(tdate,'%d/%m/%Y')>=STR_TO_DATE('$lastsunday','%d/%m/%Y') and
STR_TO_DATE(tdate,'%d/%m/%Y')<=STR_TO_DATE('$nextsunday','%d/%m/%Y') and mand=trim('$mand')"),0);
$weekper = 100 - round(($minper/$maxper) * 100) ; // calculating percentage
$updqry = "UPDATE fortnight SET percen='$weekper' WHERE mandal='$mand' and fromdt='$lastsunday' and todate='$nextsunday'";
$iconn->query($updqry); /* A simple Update through PHP */
$i++; $sl++;
} // End of While loop
$iconn->close();
} // End of If condition fortnight having zero records for the week dates