Wednesday, February 22, 2017

SHOW HIDE DIV

<div id='content'>Hello World</div>
<input type='button' id='hideshow' value='hide/show'>
jQuery:
jQuery(document).ready(function(){
    jQuery('#hideshow').live('click', function(event) {        
         jQuery('#content').toggle('show');
    });
});
For versions of jQuery 1.7 and newer use
jQuery(document).ready(function(){
        jQuery('#hideshow').on('click', function(event) {        
             jQuery('#content').toggle('show');
        });
    });

Thursday, February 16, 2017

Get Time Ago in PHP

 //timeAgo Function
  public function timeAgo($time_ago){

   $time_ago = strtotime($time_ago);
   $cur_time   = time();
   $time_elapsed   = $cur_time - $time_ago;
   $seconds    = $time_elapsed ;
   $minutes    = round($time_elapsed / 60 );
   $hours      = round($time_elapsed / 3600);
   $days       = round($time_elapsed / 86400 );
   $weeks      = round($time_elapsed / 604800);
   $months     = round($time_elapsed / 2600640 );
   $years      = round($time_elapsed / 31207680 );
   // Seconds
   if($seconds <= 60){
       return "just now";
   }
   //Minutes
   else if($minutes <=60){
       if($minutes==1){
           return "one minute ago";
       }
       else{
           return "$minutes minutes ago";
       }
   }
   //Hours
   else if($hours <=24){
       if($hours==1){
           return "an hour ago";
       }else{
           return "$hours hrs ago";
       }
   }
   //Days
   else if($days <= 7){
       if($days==1){
           return "yesterday";
       }else{
           return "$days days ago";
       }
   }
   //Weeks
   else if($weeks <= 4.3){
       if($weeks==1){
           return "a week ago";
       }else{
           return "$weeks weeks ago";
       }
   }
   //Months
   else if($months <=12){
       if($months==1){
           return "a month ago";
       }else{
           return "$months months ago";
       }
   }
   //Years
   else{
       if($years==1){
           return "one year ago";
       }else{
           return "$years years ago";
       }
   }
  }
 

Wednesday, February 15, 2017

Ignore case insensitive in php

<?php
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
    echo '$var1 is equal to $var2 in a case-insensitive string comparison';
}
?>

Thursday, February 9, 2017

Prevent session from expiring in ajax

PHP: index.php

<?php
session_start();

$_SESSION['username'] = "Raymond";
$_SESSION['status']  = "Active";

?>
<html>

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>


<body>
<form method="post">

<?php echo $_SESSION['status']; ?> 
<input type="submit" value="submit"/>
</form>
</body>

</html>
<script>
//REFRESH SESSION


setInterval(function()

$.ajax({
        url:'refresh_session.php',
        success:function(response){
           alert(response);
        }
    });
}, 6000);//time in milliseconds 
    </script>


PHP: refresh_session.php

<?php
session_start();

// store session data
if (isset($_SESSION['username'])) $_SESSION['username'] = $_SESSION['username']; 
if (isset($_SESSION['status'])) $_SESSION['status']  = 'inactive';

echo $_SESSION['status'];


?>

Thursday, February 2, 2017

Extract table from html to excel

<?php
session_start();
ob_start();
if(isset($_POST['EXTRACT'])){

$file = 'outpputfile.xls';

}

?>
<html>
<body>
<form action="" method="post">
<!-- some body here... -->

<!--------------------------------------------------------------------------------------- -->
<table width="700" border="1" cellpadding="0">
  <tr>
    <td>1</td>
    <td>1</td>
    <td>1</td>
    <td>1</td>
    <td>1</td>
  </tr>
  <tr>
    <td>2</td>
    <td>2</td>
    <td>2</td>
    <td>2</td>
    <td>2</td>
  </tr>
  <tr>
    <td>3</td>
    <td>3</td>
    <td>3</td>
    <td>3</td>
    <td>3</td>
  </tr>
  <tr>
    <td>4</td>
    <td>4</td>
    <td>4</td>
    <td>4</td>
    <td>4</td>
  </tr>
  <tr>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
  </tr>
</table>
<!--------------------------------------------------------------------------------------- -->
<?php
if(isset($_POST['EXTRACT'])){
$content = ob_get_contents();
ob_end_clean();
 header("Expires: 0");
 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
 header("Cache-Control: no-store, no-cache, must-revalidate");
 header("Cache-Control: post-check=0, pre-check=0", false);
 header("Pragma: no-cache");  header("Content-type: application/vnd.ms-excel;charset:UTF-8");
 header('Content-length: '.strlen($content));
 header('Content-disposition: attachment; filename='.basename($file));
 echo $content;
 exit;
 }
?>
<input type="submit" name="EXTRACT" value="EXTRACT TO EXCEL" class="buttons"></td>

<!-- end of some body here... -->
</form>
</body>
</html>

Date - Date in Javascript





HTML
<form method="post"  name="fillup">

<input type="text"  placeholder="yyyy/mm/dd" name="filing_deadline" onchange="getStatus()">
<input type="text" placeholder="yyyy/mm/dd"  name="actual_date_of_filling" onchange="getStatus()" >
<input type="text" readonly name="filing_status" onchange="getStatus()">

</form>

JAVASCRIPT

<script type="text/javascript" >
function getStatus() {
var actual = new Date(document.fillup.actual_date_of_filling.value);
var deadline = new Date(document.fillup.filing_deadline.value);
var timeDiff = deadline.getTime() - actual.getTime();
var DaysDiff = timeDiff / (1000 * 3600 * 24);
var stat="";
if(DaysDiff==0){
stat="Within the deadline";
}else if(DaysDiff>0){
if(DaysDiff>1) stat=DaysDiff+" Days Advance";
else stat=DaysDiff+" Day Advance";
}else if(DaysDiff<0){
   if(Math.abs(DaysDiff)>1) stat=Math.abs(DaysDiff)+" Days Late";
else stat=Math.abs(DaysDiff)+" Day Late";
}else{
stat="";
}
 document.fillup.filing_status.value=stat;
}
<script>