1. MySQL Database connection using PHP.
<?php
$config = array('host'=>'localhost', 'user'=>'webuser', 'pass'=>'123', 'databse'=>'class');
$link=mysql_connect($config['host'],$config['uname'],$config['pass']) or die("Database Connection Failed".mysql_error());
mysql_select_db($config['database'], $link) or die("database cannot be selected".mysql_error());
?>
2. Display thumbnail image from youtube or vimeo video.
function video_image($url){
$image_url = parse_url($url);
if($image_url['host'] == 'www.youtube.com' ||
$image_url['host'] == 'youtube.com'){
$array = explode("&", $image_url['query']);
return "http://img.youtube.com/vi/".substr($array[0], 2)."/0.jpg";
}else if($image_url['host'] == 'www.youtu.be' ||
$image_url['host'] == 'youtu.be'){
$array = explode("/", $image_url['path']);
return "http://img.youtube.com/vi/".$array[1]."/0.jpg";
}else if($image_url['host'] == 'www.vimeo.com' ||
$image_url['host'] == 'vimeo.com'){
$hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/".
substr($image_url['path'], 1).".php"));
return $hash[0]["thumbnail_medium"];
}
}
<img src="<?php echo video_image('youtube URL'); ?>" />
3. PHP function to get age for date of birth.
function age_from_dob($dob){
$dob = strtotime($dob);
$y = date('Y', $dob);
if (($m = (date('m') - date('m', $dob))) < 0) {
$y++;
} elseif ($m == 0 && date('d') - date('d', $dob) < 0) {
$y++;
}
return date('Y') - $y;
}
echo age_from_dob('2005/04/19'); date in yyyy/mm/dd format.
4. Random password generation using PHP.
(1)
echo substr(md5(uniqid()), 0, 8);
(2)
function rand_password($length){
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$chars .= '0123456789' ;
$chars .= '!@#%^&*()_,./<>?;:[]{}\|=+';
$str = '';
$max = strlen($chars) - 1;
for ($i=0; $i < $length; $i++)
$str .= $chars[rand(0, $max)];
return $str;
}
echo rand_password(16);
5. Get date difference PHP.
date_default_timezone_set("Asia/Calcutta");
function dt_differ($start, $end){
$start = date("G:i:s:m:d:Y", strtotime($start));
$date1=explode(":", $start);
$end = date("G:i:s:m:d:Y", strtotime($end));
$date2=explode(":", $end);
$starttime = mktime(date($date1[0]),date($date1[1]),date($date1[2]),
date($date1[3]),date($date1[4]),date($date1[5]));
$endtime = mktime(date($date2[0]),date($date2[1]),date($date2[2]),
date($date2[3]),date($date2[4]),date($date2[5]));
$seconds_dif = $starttime-$endtime;
return $seconds_dif;
}
// call the function
$today = date("Y-n-j H:i:s");
$fromday = "2012-12-31 23:59:59";
$timediffer = dt_differ($fromday, $today);
echo $timediffer." seconds";
6.Zip Multiple Files and Download
This script will zip multiple files and force download created zip file. This function required Zip Archive enable in your server.
<?php
function zipFilesDownload($file_names,$archive_file_name,$file_path){
$zip = new ZipArchive();
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
foreach($file_names as $files){
$zip->addFile($file_path.$files,$files);
}
$zip->close();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;
}
$fileNames=array('files/file1.docx','files/file1.pdf');
$zip_file_name='myFile.zip';
$file_path=dirname(__FILE__).'/';
zipFilesDownload($fileNames,$zip_file_name,$file_path);
?>
Unzip Files in Web server
<?php
$zip = zip_open("moooredale.zip");
if ($zip) {
while ($zip_entry = zip_read($zip)) {
$fp = fopen(zip_entry_name($zip_entry), "w");
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,"$buf");
zip_entry_close($zip_entry);
fclose($fp);
}
}
zip_close($zip);
}
?>