Stella inattivaStella inattivaStella inattivaStella inattivaStella inattiva
 

Some useful snippets for the Hypertext Preprocessor or better knows as PHP.

 

Some useful snippets for the Hypertext Preprocessor or better knows as PHP.

 

Older than

 Testing if a date is older than n days

<?php
date_default_timezone_set('Europe/Rome');
if(strtotime('2014-11-15 17:14:40')<strtotime('-30 days')){
   echo "I am older than 30 days"; 
 } else {
   echo "I am too young, less than 30 days";  
}
?>

 

Command Line Argument

 A way to read the command line argument

<?php
// testarg.php
// php testarg days=180 parse_str(implode('&', array_slice($argv, 1)), $_GET); echo $_GET['days'] ; ?>

 

Elapsed Time

 Calculate the elapsed time between two date time

<?php
date_default_timezone_set('Europe/Rome');
 
 // Elapsed time - START
$time_start = microtime(true);
for ($x = 0; $x <= 10000000; $x++) {
    
} 
sleep(1.5);

// Elapsed time - END 
$timelapsed=elapsed($time_start);
echo "Elapsed Time: ".$timelapsed;
return "";

/** *******************************************************************************
Calculate a precise time difference. 
	@param string $start result of microtime() 
	@param string $end result of microtime(); if NULL/FALSE/0/'' then it's now 
	@return flat difference in seconds, calculated with minimum precision loss 
*/ 
function elapsed( $start, $end=NULL ) {
	if( !$end ) { 
	    $end= microtime(); 
	}	
	list($start_usec, $start_sec) = explode(" ", $start); 
	list($end_usec, $end_sec) = explode(" ", $end); 
	$diff_sec= intval($end_sec) - intval($start_sec); 
	$diff_usec= floatval($end_usec) - floatval($start_usec); 
	return floatval( $diff_sec ) + $diff_usec; 
}   
?>	

 

Get Image Size

Get image height, width and attribute

list($width, $height, $type, $attr) = getimagesize($URLImage); 	
echo  'data-size="'.$width."x".$height.'"';	

 

New Line for command line and web

 

New  Line for command line and web

// newline for CLI and WEB
public function newline() {
        if (PHP_SAPI === 'cli'){ 
		   return PHP_EOL;
		} else{
		   return "<br/>";
		}
 }

// or a simpler way simple
$br = PHP_SAPI === 'cli' ? "\n" : "<br/>";

 

 

PHP Script is running on Linux or Windows?

 

 PHP Script is running on Linux or Windows?

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    echo 'This is a server using Windows!';
} else {
    echo 'This is a server not using Windows!';
}

Force the browser to write text like the CLI session

 

Force the browser to write text like the CLI session

header("Content-type: text/plain");

 

Verify if a class already available

 

Verify if a class already available

if (!class_exists('ArtResponsiveMenuHelper')) {
	class ArtResponsiveMenuHelper {
...
}

 

Retuning multiple parameters

 

Retuning multiple parameters

// Returning an array
function wtf($blahblah = true) {
    $var1 = "ONe";
    $var2 = "tWo";

    if($blahblah === true) {
      return $var2;
    }

    if($blahblah == "both") {
      return array($var1, $var2);
    }

    return $var1;
}

echo wtf("both")[0]
//would echo: ONe
echo wtf("both")[1]
//would echo: tWo

list($first, $second) = wtf("both")
// value of $first would be $var1, value of $second would be $var2

 

 

 

 

HEREDOC to assign string

 

Assigning HTML contents to PHP variables

$str = <<<HTML
<div id="div1">
    <div id="div10"></div>
    <div id="div11"></div>
    <div id="div12"></div>
</div>
HTML;

 

 

 

New Line based on CLI or WEB

 

New Line based on CLI or WEB

// 1st way - short function br()
function br() { return (PHP_SAPI === 'cli' ? "\n" : "<br />"); }

// 2nd way - var
$br = PHP_SAPI === 'cli' ? "\n" : "<br/>";

 more on stackoverflow

 

 

 

 

 

To Be continued

To Be Continued... 

Stay tuned, I will add more snippets soon...

 

 

DISQUS - Leave your comments here