<?php echo $_SERVER['HTTP_REFERER'] //will return the previous page ?>
How to find out the difference between two given dates – PHP
<?php $startDate=explode("-", "02-08-2010"); $endDate=explode("-", "26-04-2007"); echo gregoriantojd($startDate[1], $startDate[0], $startDate[2]) - gregoriantojd($endDate[1], $endDate[0], $endDate[2]); //1194 ?>
How to disable error reporting in php?
<?php error_reporting(0);// Will not report any error error_reporting(-1);// Shows all errors ?>
Limits the maximum execution time – PHP
<?php set_time_limit(2); // // If the value is "0" , it will rum for unknown time ?>
PHP script to download the PDF file
<?php $file = 'testFile.pdf'; // file to be downloaded 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/pdf"); header('Content-length: '.filesize($file)); header('Content-disposition: attachment; filename='.basename($file)); readfile($file); exit; ?>
Create and download excel file – PHP
<?php $file = 'excelFile-'.date("Y-M-D")."-".time().'.xls'; ob_start(); echo '<table border="1"> '; echo '<tr><th>Product Name </th><th>Category</th><th>Active</th></tr>'; for ($i=0; $i<=10; $i++) { echo "<tr><td>Product$i</td><td align='center'>Category $i</td><td>Active</td></tr>"; } echo '</table>'; $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; ?>
Image resize script – PHP
<?php class SimpleImage { function resizeImage($max_width,$max_height,$url,$destination){ $quality = 95; $source_pic = ''.$url.''; $src = imagecreatefromjpeg($source_pic); list($width,$height)=getimagesize($source_pic); $x_ratio = $max_width / $width; $y_ratio = $max_height / $height; if( ($width <= $max_width) && ($height <= $max_height) ){ $tn_width = $width; $tn_height = $height; } elseif (($x_ratio * $height) < $max_height){ $tn_height = ceil($x_ratio * $height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height; } $tmp=imagecreatetruecolor($tn_width,$tn_height); imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height); imagejpeg($tmp,$destination,$quality); imagedestroy($tmp); } } $image = (object) new resizeImage(); $image->resizeImage(100,80,$source,$destination); ?>
Latest tweets in your site – PHP
<?php
$username=”tppramod”; // Your Twitter username
$limit=10;//Number of tweets
if(!is_numeric($limit)){$limit = 10;}
$xml = simplexml_load_file(‘http://search.twitter.com/search.atom?q=from%3A’.urlencode($username));?>
<table>
<?php for($i=0;$i<$limit;$i++){
if(empty($xml->entry[$i]->content)) break;
$attr = $xml->entry->link[1]->attributes();
?>
<tr> <td> <img src=”<?php echo $attr[‘href’]; ?>” /> </td> <td style=” font-size:12px;”> <?php echo $xml->entry[$i]->content; ?> </td> </tr>
<?php } ?>
</table>
Downgrade PHP from 5.3 to 5.2 – PHP
http://ubuntuforums.org/archive/index.php/t-1459163.html
Strip HTML tags except the given – PHP
<?php $text = '<p>TestP</p><b>Tesb</b> <a href="">TestA</a>'; echo strip_tags($text); //Strip all tags echo strip_tags($text, '<p><a>'); //Strip all tags except p and a ?>