Thursday, 13 August 2015

working with PHP and MYSQL dates

$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

creating dynamic arrays and dynamic variables example in php

<?php

$test = array();                                           //  The Dynamic array declaration
$i=0;
$nl = array("abc","yzc","ijk");                  // Static array

for($i=0;$i<3;$i++){
echo $nl[$i]."\n";                                // Displaying static array
}

/* Gives out put as
abc
yzc
ijk


*/

for($i=0;$i<3;$i++){
$test[$nl[$i]]="number:".$i + 1;      
${"file" . $i} = "test $i";

}

var_dump($test);

/* Gives output as

array(3) { ["abc"]=> int(1) ["yzc"]=> int(1) ["ijk"]=> int(1) } 

*/


echo "<br/>".$test["yzc"];     // Output is one i.e., 1

for($i=0; $i<=2; $i++) {
echo "<br/>".${"file".$i};
}

/* Gives output as

test 0
test 1
test 2


*/

?>