Wednesday 30 July 2014

Php Snippets


Php Program for getting current goldrate

<?php
$price = 0;
$tenK = 0;
$fourteenK = 0;
$twentyFourK = 0;
$gs = 0;
$goldSpot = 0;
$content = file_get_contents('http://www.xmlcharts.com/cache/precious-metals.php?format=json'); 
$metals = json_decode($content);
$price = $metals->inr->gold;
$tenK= round(((41.7/100)*$price),2);
$fourteenK= round(((58.5/100)*$price),2);
$eighteenK= round(((75/100)*$price),2);
$twentyK= round(((83.3/100)*$price),2);
$twentytwoK= round(((91.8/100)*$price),2);
$twentyFourK= round((1*$price),2);
?>
Output :
KaratPrice
24KRs.2507.18
22KRs.2301.59
20KRs.2088.48
18KRs.1880.38
14KRs.1466.7

How to display post by particular category with direct sql query?

SELECT *
FROM `wp_postmeta` AS wpmt
INNER JOIN wp_postmeta AS pmt
INNER JOIN wp_posts AS pst
WHERE pmt.meta_key = '_thumbnail_id'
AND wpmt.meta_key = '_wp_attached_file'
AND pmt.meta_value = wpmt.post_id
AND pst.post_type = 'post'
AND pmt.post_id = pst.ID
AND pmt.post_id
IN (

SELECT pst.ID
FROM wp_terms
INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr ON wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_posts p ON p.ID = wpr.object_id
WHERE taxonomy = 'category'
AND p.post_type = 'post'
AND wp_terms.name = 'ISO'
ORDER BY object_id
)

Here i have use category name iso hence i give wp_terms.name = 'ISO'

Usefull Urls for php coders

Convert php values to currency

International format (Germany) with 2 decimals:
<?php
$number = 1234.56;
setlocale(LC_MONETARY,"de_DE");
echo money_format("%.2n", $number);
?>
The output of the code above will be: 1 234,56 EUR

Difference between two days

<?php
$date1 = date('y-m-d h:i:s');//today date
$date2 = date('Y-m-d h:i:s', strtotime($tvalue->billdate.' + 2 day')); // after 2 days
$diff = strtotime($date2) - strtotime($date1);// subtract 2 days
$diff_in_hrs = $diff/3600;
$diff_in_sec = $diff_in_hrs*3600;
echo gmdate("H:i:s", $diff_in_sec);// remaining hours
?>

Email send for html format in php

<?php
$headers  = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: ' . $from_email;
//$headers .= "\r\nReply-To: " . $from_email;
//$headers .= "\r\nX-Mailer: PHP/" . phpversion();
$to = $emailid;
$subject= 'Customer Billing Details';
$message   = '<html><body><div>';
$message  .= '<h2>Our mail</h2>';
$message  .= 'A message to our Customers';
$message  .= '<a href="http://dev.probytes.net/uncer/index.php/?option=com_users&view=billing&rando='.$randno.'">Click to get your bill</a>';
$message  .= '</div></body></html>';
mail($to, $subject, $message, $headers);
?>