<?php
function get_age($birth_date){
return floor((time() - strtotime($birth_date))/31556926);
}
echo " I am ".get_age("2000-05-10") ." years old";
?>
$path = $_FILES['image']['name']; $ext = pathinfo($path, PATHINFO_EXTENSION); echo $ext;
<?php $x = 100; $y = "100"; if($x == $y){ echo "Yes"; }else{ echo "No"; } ?>
<?php $x = 100; $y = "100"; if($x === $y){ echo "Yes"; }else{ echo "No"; } ?>
<?php /** * Grabs and returns the URL of current page. * @param none * @return URL of current page */ function grabCurrentURL(){ if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") { $url = "https://"; }else{ $url = "http://"; } $url .= $_SERVER['SERVER_NAME']; if($_SERVER['SERVER_PORT'] != 80){ $url .= ":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; }else{ $url .= $_SERVER["REQUEST_URI"]; } return $url; } echo grabCurrentURL(); ?>
<?php /** * Generates random passwords. * @param int Password Length (Default : 10) * @return Random String * @author Harshal Limaye (http://coffeecupweb.com/) */ function genPass($len = 10) { $charPool = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789"; $pass = array(); $length = strlen($charPool) - 1; for ($i = 0; $i < $len; $i++) { $n = rand(0, $length); $pass[] = $charPool[$n]; } return implode($pass); } echo genPass(); ?>
<?php //extract a file with php function extractZip($src,$dest){ $zip = new ZipArchive; $res = $zip->open($src); if ($res === TRUE) { $zip->extractTo($dest); $zip->close(); echo 'Files Extracted Successfully!'; } else { echo 'Extraction Failed!'; } } $src = "zip/file.zip"; $dest = "extractedfile/1/"; echo extractZip($src,$dest); ?>
//Creating a Zip File Using PHP function genZip($files = array(),$zipName){ $zip = new ZipArchive(); $zip->open($zipName.'.zip', ZipArchive::CREATE); foreach($files as $file){ $zip->addFile($file); } $zip->close(); }
//Usage of genZip function $files = array( 'file1.pdf', 'file2.pdf', 'file3.pdf', 'folder2/file4.pdf', 'folder2/file5.pdf' ); $zipName = 'myfiles'; genZip($files,$zipName);
//get video id from youtube url function getYouTubeId($url){ parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars ); return $my_array_of_vars['v']; } echo getYouTubeId('https://www.youtube.com/watch?v=y2Ky3Wo37AY');
y2Ky3Wo37AY
<?php //Connect and select a database mysql_connect('localhost','root',''); mysql_select_db('webapp'); //Output headers to make file downloadable header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=spreadsheet.csv'); //Write the output to buffer $data = fopen('php://output', 'w'); //Output Column Headings fputcsv($data,array('Name','Email Address')); //Retrieve the data from database $rows = mysql_query('SELECT name, email FROM users'); //Loop through the data to store them inside CSV while($row = mysql_fetch_assoc($rows)){ fputcsv($data, $row); }
public function stringToSlug($string){ //Remove white spaces from both sides of a string $string = trim(string); //Lower case everything $string = strtolower($string); //Make alphanumeric (removes all other characters) $string = preg_replace("/[^a-z0-9_\s-]/", "", $string); //Clean up multiple dashes or whitespaces $string = preg_replace("/[\s-]+/", " ", $string); //Convert whitespaces and underscore to dash $string = preg_replace("/[\s_]/", "-", $string); return $string; }
$string = "a, b, c, d,"; $filtered = rtrim($string, ','); echo $filtered;
a, b, c, d
<?php echo date("Y-m-d", time() - (60*60*24) ); ?> <?php echo date("Y-m-d", strtotime("-1 day")); ?> <?php echo date("Y-m-d", strtotime("yesterday")); ?> <?php echo date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")-1,date("Y"))); ?>
<?php echo abs(-6.2); // 6.2 (float) echo abs(19); // 19 (integer) echo abs(-2); // 2 (integer) ?>
if (empty($date)) { return "Please enter a date!"; } $duration_in = [ "second", "minute", "hour", "day", "week", "month", "year", "decade" ]; $len = [ "60", "60", "24", "7", "4.35", "12", "10" ]; $now = time(); $unixDate = strtotime($date); if (empty($unixDate)) { return "Please enter a valid date!"; } if ($now > $unixDate) { $diff = $now - $unixDate; $tense = "ago"; } else { $diff = $unixDate - $now; $tense = "from now"; } for ($j = 0; $diff >= $len[$j] && $j < count($len) - 1; $j++) { $difference /= $len[$j]; } $diff = round($diff); if ($diff != 1) { $duration_in[$j].= "s"; } return "$diff $duration_in[$j] {$tense}"; }
echo time_ago($date);
<?php function ccw_remove_empty_element_from_array($data) { foreach ($data as $key => $value) { if (is_array($value)) { $data[$key] = ccw_remove_empty_element_from_array($data[$key]); } if (empty($data[$key])) { unset($data[$key]); } } return $data; }
<?php // PHP 5.3- function birthday($birthday){ $age = strtotime($birthday); if($age === false){ return false; } list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age)); $now = strtotime("now"); list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now)); $age = $y2 - $y1; if((int)($m2.$d2) < (int)($m1.$d1)) $age -= 1; return $age; } echo birthday('1981-05-18'); // PHP 5.3+ function birthday($birthday) { $age = date_create($birthday)->diff(date_create('today'))->y; return $age; } echo birthday('1981-05-18');
?>
Hello Friends! I am Ramana a part time blogger from Hyderabad.