Monday, June 27, 2016

Get / display all dates between two dates-php

It's very common uses to get all dates between two dates rang.

However php has a own class callled "DatePeriod" class which made our work easy for this task.


 $begin = new DateTime('2016-06-01');  
 $end = new DateTime('2016-06-28');  
 $end = $end->modify( '+1 day' );   
 $daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);  
 foreach($daterange as $date){  
   echo $date->format("Y-m-d") . "<br>";  
 }  

Note :
 //(P1D stands for period of one day, see DateInterval for further documentation)
 //$end = $end->modify( '+1 day' ); mean dates include the last day 2016-06-28  

Output will looks like
2016-06-01
2016-06-02
2016-06-03
2016-06-04
2016-06-05

Saturday, May 28, 2016

Decodes a complex JSON string and converted into associative arrays using php

Call a API 
 $jsonurl = "https://api.coindesk.com/v1/bpi/currentprice/USD.json";  
 $json = file_get_contents($jsonurl); 
 echo var_dump(json_decode($json)); 

Which will return this

 object(stdClass)[5]  
  public 'time' =>   
   object(stdClass)[6]  
    public 'updated' => string 'May 29, 2016 06:05:00 UTC' (length=25)  
    public 'updatedISO' => string '2016-05-29T06:05:00+00:00' (length=25)  
    public 'updateduk' => string 'May 29, 2016 at 07:05 BST' (length=25)  
  public 'disclaimer' => string 'This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org' (length=155)  
  public 'bpi' =>   
   object(stdClass)[7]  
    public 'USD' =>   
     object(stdClass)[8]  
      public 'code' => string 'USD' (length=3)  
      public 'rate' => string '519.1350' (length=8)  
      public 'description' => string 'United States Dollar' (length=20)  
      public 'rate_float' => float 519.135  

Or as plain json return

 {"time":{"updated":"May 29, 2016 03:59:00 UTC","updatedISO":"2016-05-29T03:59:00+00:00","updateduk":"May 29, 2016 at 04:59 BST"},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org","bpi":{"USD":{"code":"USD","rate":"516.4050","description":"United States Dollar","rate_float":516.405}}}  
Now we want to get data as associative arrays

 $jsonurl = "https://api.coindesk.com/v1/bpi/currentprice/USD.json";  
 $json = file_get_contents($jsonurl);  
 $obj = json_decode($json);  
 echo $obj->bpi->USD->{'rate'}; // show the rate   

Thursday, May 12, 2016

Popup new window center screen php javascript

First Create a javascript function for popup window. w=width , h=height of the window
 // Popup windows   
 function popupwindow(url, title, w, h) {  
        var left = (screen.width/2)-(w/2);  
        var top = (screen.height/2)-(h/2);  
        return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);  
 }  

Now make a php function to use this java-script function everywhere in php easily.

 /**  
  * Return pop up   
  * @author Sharif Ahmed  
  * @param str url  
  * @param str title/value  
  * @param int width  
  * @param int height  
  * @return return popup center windows  
  */  
 function my_anchor_popup($url,$title,$width,$height)   
   {  
     return '<a href="'.$url.'" onclick="popupwindow(\''.$url."','".$title."', '".$width."','".$height."'); return false;\">".$title.'</a>';  
   }   

Total Pageviews