Adding divs or any design patterns to a foreach loop every 4 or any times

foreach (array_chunk($posts, 4, true) as $pts) {
echo ‘</section>  or div or tr anything’;
foreach($pts as $t) {
echo $t->ID;
}
echo ‘</section>  or div or tr anything’;
}

Instead of doing mod split up on each foreach this method is clean and easy.

 

 

Advertisements

Get Country currency code by user IP address in php

$ip=$_SERVER[‘REMOTE_ADDR’];
$countrycode = “http://www.geoplugin.net/json.gp?ip=$ip&#8221;;
$ip_data = @json_decode(file_get_contents(“http://www.geoplugin.net/json.gp?ip=&#8221;.$ip));

if($ip_data && $ip_data->geoplugin_countryName != null){
$result[‘geoplugin_countryName’] = $ip_data->geoplugin_countryName;
$result[‘geoplugin_currencyCode’] = $ip_data->geoplugin_currencyCode;
}

echo $result[‘geoplugin_currencyCode’];

Currency Converter in PHP

$encode_amount = 1;
$amount = 100;
$from_Currency = ‘INR’;
$to_Currency = ‘USD’;

$get = file_get_contents(“https://www.google.com/finance/converter?a=$encode_amount&from=$from_Currency&to=$to_Currency&#8221;);
$get = explode(“<span class=bld>”,$get);
$get = explode(“</span>”,$get[1]);
$rate= preg_replace(“/[^0-9\.]/”, null, $get[0]);
$converted_amount = $amount*$rate;
$data = array( ‘rate’ => $rate, ‘converted_amount’ =>$converted_amount, ‘from_Currency’ => strtoupper($from_Currency), ‘to_Currency’ => strtoupper($to_Currency));

 

Array
(
    [rate] => 0.0147
    [converted_amount] => 1.47
    [from_Currency] => INR
    [to_Currency] => USD
)

 

 

PHP Functions to Remember

Function List :

 sleep()

Sleep function is used to Stop the execution of the code for certain amount of seconds which we pass

Example :

sleep(5); this will stop the code execution for 5 seconds and then starts executing

unsleep() function is reverse of sleep function

CURL in PHP (Client’s Uniform Resource Locator)

Curl is used to fetch the Remote content in to a file in  HTML format

<?php
$ch = curl_init(“http://www.google.com/&#8221;);
$fp = fopen(“sample.txt”, “w”);

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

The Above code will retrieve content in HTML format from ‘www.google.com’ and store in the file named ‘sample.txt’.

 

CONVERSION OF A STRING TO THE DESIRED DATE FORMAT USING PHP

  1. Strtotime:

Strtotime function will be helpful only if the string is in the following format.

$date=”31-01-2016″; // Assuming  the string to be Jan 31, 2016

date(“Y-M-d”, strtotime ($date));

Result: 2016-Jan-31

$date=”01-31-2016″; // Assuming  the string to be Jan 31, 2016

date(“Y-M-d”, strtotime ($date));

Result: 1970-Jan-01

Strtotime fails to help in this scenario.

2. Mktime:

Using mktime, we can achieve the desired format as follows:

Syntax: $time=mktime(hour,minute,second,month,day,year,is_ds);

Example:

$time=mktime(0,0,0,01,31,2016,0);

date(‘Y-M-d’,$time);

Result: 2016-Jan-31

However, you cannot explode the string and pass the values in the corresponding places as it lengthens coding and execution time.

3. DateTime::createFromFormat

$date1=”01-31-2016”;

$date = DateTime::createFromFormat(‘m-d-Y’, $date1);// I am assuming that my string is in m-d-Y format

$date->format(‘Y-m-d’); 

Result: 2016-Jan-31

If my string is in d-m-Y format:

$date = DateTime::createFromFormat(‘d-m-Y’, $date1);// I am assuming that my string is in m-d-Y format

$date->format(‘Y-m-d’); 

Result: 2016-Jan-31

Unlike strtotime, the above helps in all scenarios and thus proves effective.