5 useful PHP functions

Hi, 

These days I’m concebtration on PHP then desktop developing tools, and I only follow http://php.net/manual/en/index.php for guidance, It has wonderful help tips for beginners. From that I’ve found some very useful PHP fuunctions and I would like to share them with u

strftime()

Date is very importent part in a web app or widget or just a simple site, so weshould be able to output them in any format we need. These days I was working with a community to develop a prayer times web app for them, This helped me a lot, I can say this function solved 90% of the puzzles I had and helped me well in making of that app. In PHP we can get the time-stamp easily with time() but for customized time and date display we can use this strftime()

strftime("%d %B %Y", time()); // 29 August 2012 

array_rand()

This function is used to give a rendom value in an array

$numbers= ["one", "two", "three", "four"];

$x = array_rand($numbers);

$numbers[$x];

You might use rand() or mt_rand() to get a random number, passing 0 and the last index of the array as the min and max parameters; this will give you a random key that you can use to pull a value from your array. Hoe ever this array_rand() is specialized for arrays and quicker then rand() or mt_rand().

date_default_timezone_set()

The date_default_timezone_set() function sets the default timezone used by all date/time functions.

date_default_timezone_set('Asia/Colombo'); //time zome comes colombo

Here is some useful code to set the default timezone to the user’s local timezone. 

function getLocalTimezone()

{   

$iTime = time();   

$arr = localtime($iTime);   

$arr[5] += 1900;    

$arr[4]++;   

$iTztime = gmmktime($arr[2], $arr[1], $arr[0], $arr[4], $arr[3], $arr[5], $arr[8]);   

$offset = doubleval(($iTztime-$iTime)/(60*60));   

$zonelist =     array   

(       

'Kwajalein' => -12.00,     

'Pacific/Midway' => -11.00,       

'Pacific/Honolulu' => -10.00,       

'America/Anchorage' => -9.00,       

'America/Los_Angeles' => -8.00,       

'America/Denver' => -7.00,       

'America/Tegucigalpa' => -6.00,       

'America/New_York' => -5.00,       

'America/Caracas' => -4.30,       

'America/Halifax' => -4.00,       

'America/St_Johns' => -3.30,       

'America/Argentina/Buenos_Aires' => -3.00,       

'America/Sao_Paulo' => -3.00,       

'Atlantic/South_Georgia' => -2.00,       

'Atlantic/Azores' => -1.00,       

'Europe/Dublin' => 0,       

'Europe/Belgrade' => 1.00,       

'Europe/Minsk' => 2.00,     

 'Asia/Kuwait' => 3.00,       

'Asia/Tehran' => 3.30,       

'Asia/Muscat' => 4.00,       

'Asia/Yekaterinburg' => 5.00,       

'Asia/Kolkata' => 5.30,       

'Asia/Katmandu' => 5.45,       

'Asia/Dhaka' => 6.00,       

'Asia/Rangoon' => 6.30,       

'Asia/Krasnoyarsk' => 7.00,       

'Asia/Brunei' => 8.00,       

'Asia/Seoul' => 9.00,       

'Australia/Darwin' => 9.30,       

'Australia/Canberra' => 10.00,       

'Asia/Magadan' => 11.00,       

'Pacific/Fiji' => 12.00,       

'Pacific/Tongatapu' => 13.00    );   

$index = array_keys($zonelist, $offset);   

if(sizeof($index)!=1)     

return false;   

return $index[0];} 

date_default_timezone_set(getLocalTimezone());

?>

This is derived from PHP manual site.

strip_tags()

It’s not uncommon to accept larger chunks of text from a user: maybe as a comment or a bio. Of course, we probably don’t want just any HTML tags to be allowed in that text, though, right? we don’t want random JavaScript running, or perhaps your styling dictates they only use plain text. So, we’ll want to strip out the HTML tags they enter, using strip_tags:

$message = "Hello Facebook";

echo strip_tags($message); // "Hello Facebook"

Advanced usage is

$message = "<div> This is <strong>my</strong> bio </div>";  

echo strip_tags($message"<strong><em><code>"); // "This is <strong>my</strong> bio" 

isset()

This is a handy way to check if a variable has been set,  use the isset function. We just pass it a variable name, and it will return true if that variable exists, and is set to something other than NULL.

$name = "Joe";

isset($name); // trueisset($age); // false

if(isset($_GET['query']))

  // get results and display them

}

else

{   

  // show some default content

}

Thats all. I hope u could get something from this, alteast boring.. LOL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s