Tuesday, February 26, 2013

[PHP] How to get time difference in day, hour, minute like what the facebook timestamp do?

Here's how to get the time difference between the given time and the present time.

If the time difference is not exceeding  7 DAYS, we will display time difference like
Monday at 12:30pm
Tuesday at 12:30pm
Wednesday at 12:30pm
Thursday at 12:30pm
Friday at 12:30pm
Saturday at 12:30pm
Sunday at 12:30pm
and so on...

If the time difference is not exceeding  24 HOURS, we will display time difference like
23 Hours ago
19 Hours ago
15 Hours ago
8 Hours ago
1 Hour ago
and so on...

If the time difference is not exceeding  60 MINUTES, we will display time difference like
59 minutes ago
47 minutes ago
33 minutes ago
25 minutes ago
18 minutes ago
1 minute ago
and so on...

If the time difference is not exceeding  60 SECONDS, we will display time difference like
59 seconds ago
47 seconds ago
33 seconds ago
25 seconds ago
18 seconds ago
1 second ago
and so on...


So here is the  code

<?php

//$sample_date came from profile.php

$now = date('Y-m-d H:i:s');

$sample_date = "2013-02-23 17:30:25";        //year-month-day hour:minute:second

$start_date = new DateTime($sample_date);

$since_start = $start_date->diff(new DateTime($now));

$year = $since_start->y;

$month = $since_start->m;

$day = $since_start->d;

$hour = $since_start->h;

$minute = $since_start->i;

$second = $since_start->s;

$date = strtotime($sample_date);

$time = date("g:ia", strtotime($sample_date));

if($day != 0)

{

    if($day == 1)        //yesterday

    {     

        $displayDate = "Yesterday at $time";

    }

    else if(($day > 1) && ($day < 8))        //day of the week

    {     

        $displayDate = date('l', $date) . " at $time";

    }

    else        //month

    {

        $yearAppend = (date('Y', $date) != date('Y'))? ", " . date('Y', $date) : "";

         

        $displayDate = date('F j', $date) . $yearAppend; 

    }

}

else

{

 

     

    if($hour != 0)

    {

        $s = ($hour>1)? "s" : "";

        $displayDate = "$hour hour$s ago";

    } 

    else if($minute != 0)

    {

        $s = ($minute>1)? "s" : "";

        $displayDate = "$minute minute$s ago";

    }

    else

    {

        $s = ($second>1)? "s" : "";

        $displayDate = "$second second$s ago";

    }

}

echo "$displayDate";

?>

Thursday, February 21, 2013

[PHP] Retrieving photos after you save it using facebook graph api.

In this tutorial, I will assume that you already create a code that will upload photos from your site to user's facebook account.
But if you're not and you're looking for that part, you can visit this tutorial
[PHP] Upload picture to facebook using Graph AP


Let's begin.

Things to have.
1. We need to know the title of the album where we want to get the photos. If you are done with the code on "how to upload picture to facebook using Graph API", for sure you have tested this and you know the title of the album where your uploaded photo goes.


Here are the steps we are going through.
1. We need to get the access token.
2. We need to get the id of the specific album we want to get photos.
3. We are going to get all the photos inside that album.


<?php

$app_id = "YOUR App ID/API Key";
$app_secret = "YOUR App Secret";

//Take NOTE: this must include http://
//ex. http://text2imagestatus.com/index.php
$post_login_url = "THE PAGE WHERE YOU ARE GOING TO SAVE THIS FILE";

$code = $_REQUEST["code"];

//Obtain the access_token with publish_stream permission
if (!$code)
{
    $dialog_url= "http://www.facebook.com/dialog/oauth?"
    . "client_id=" . $app_id
    . "&redirect_uri=" . urlencode( $post_login_url)
    . "&scope=publish_stream,user_photos";
    echo("<script>top.location.href='" . $dialog_url
    . "'</script>");
}
else
{

    $token_url="https://graph.facebook.com/oauth/access_token?"
    . "client_id=" . $app_id
    . "&client_secret=" . $app_secret
    . "&redirect_uri=" . urlencode( $post_login_url)
    . "&code=" . $code;
    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];


    //get the albums
    $get_album = "https://graph.facebook.com/me/albums?access_token=$access_token";
    $album = file_get_contents($get_album);
    $album = json_decode($album, true);

    //count the number of albums
    $count_album = count($album['data']);


    //loop until the title of the album reach
    for($i=0; $i<$count_album; $i++)
    {
        if($album["data"][0]["name"] == 'TITLE OF THE ALBUM')
        {
            $id_album = $album["data"][0]["id"];
            break;
        }
    }


    //get photos in the album
    $get_photo = "https://graph.facebook.com/$id_album/photos?access_token=$access_token";
    $photo = file_get_contents($get_photo);
    $photo = json_decode($photo, true);

    //count the number of photos
    $count_photo = count($photo['data']);

    //loop to get all the source url of photos
    for($i=0; $i<$count_photo; $i++)
    {
        $source = $photo["data"][$i]["source"];
        $printme .= "<img src='$source' /></br>";
    }
   
   //this will print the photos that we got from the album
   echo"$printme";

    //this will printout the array of albums and photos for you to see them
    echo '<pre>';
    print_r($album);
    print_r($photo);
    echo '</pre>';

}
?>


Wednesday, January 9, 2013

[PHP] Upload picture to facebook using Graph API

This code is intented to post photo to user's timeline using your website.
As of today: January 09, 2013 this code is 100% tested and working!



This code has 2 parts.
  • First: You can control what image you want to post like http://sharefavoritebibleverses.com/images/bible_verses.png
  • Second: The user will upload image coming from their computer file.


First Part
You can control what image you want to post like http://sharefavoritebibleverses.com/images/bible_verses.png

<?php
      $app_id = "YOUR APP ID";
      $app_secret = "YOUR APP SECRET";
     
      //Take NOTE: this must include http://
      //ex. http://sharefavoritebibleverses.com/fb_connect.php       
      $post_login_url = "THE PAGE WHERE YOU ARE GOING TO SAVE THIS FILE"; 
     
      //Take NOTE: this must include http://
      $photo_url = "http://sharefavoritebibleverses.com/images/bible_verses.png";
     
      //you can add link to your image caption, but you can't use this tag <a href='http://link.com' >visit my link</a>
      $photo_caption = "create another verse at http://sharefavoritebibleverses.com";

  $code = $_REQUEST["code"];

  //Obtain the access_token with publish_stream permission
  if (!$code)
  {
    $dialog_url= "http://www.facebook.com/dialog/oauth?"
      . "client_id=" .  $app_id
      . "&redirect_uri=" . urlencode( $post_login_url)
      .  "&scope=publish_stream";
     
    echo("<script>top.location.href='" . $dialog_url
      . "'</script>");
  }
  else
  {
    $token_url="https://graph.facebook.com/oauth/access_token?"
      . "client_id=" . $app_id
      . "&client_secret=" . $app_secret
      . "&redirect_uri=" . urlencode( $post_login_url)
      . "&code=" . $code;
     
    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];

    // POST to Graph API endpoint to upload photos
    $graph_url= "https://graph.facebook.com/me/photos?"
      . "url=" . urlencode($photo_url)
      . "&message=" . urlencode($photo_caption)
      . "&method=POST"
      . "&access_token=" .$access_token;

    echo '<html><body>';
       
       echo file_get_contents($graph_url);
   
    echo '</body></html>';
  }
?>



This will output something like this
{"id":"517833744917123","post_id":"100000713303095_517823648251456"}


you can change this        
   
    echo file_get_contents($graph_url);
   
into this    

    $ok = file_get_contents($graph_url);
       
    if($ok)
        echo'<script language="JavaScript">window.location="index.php";</script>';

       

so you can direct them to any page you want after they submitted the photo.
or you can do any conditions you want.   

**************************************************************************************************

Second Part
The user will upload image coming from their computer file.

<?php
      $app_id = "YOUR APP ID";
      $app_secret = "YOUR APP SECRET";
     
      //Take NOTE: this must include http://
      //ex. http://sharefavoritebibleverses.com/fb_connect.php       
      $post_login_url = "THE PAGE WHERE YOU ARE GOING TO SAVE THIS FILE"; 
   
       $code = $_REQUEST["code"];

       //Obtain the access_token with publish_stream permission
       if(empty($code))
       {
          $dialog_url= "http://www.facebook.com/dialog/oauth?"
           . "client_id=" .  $app_id
           . "&redirect_uri=" . urlencode( $post_login_url)
           .  "&scope=publish_stream";
          
          echo("<script>top.location.href='" . $dialog_url
          . "'</script>");
         }
        else
        {
          $token_url="https://graph.facebook.com/oauth/access_token?"
           . "client_id=" . $app_id
           . "&redirect_uri=" . urlencode( $post_login_url)
           . "&client_secret=" . $app_secret
           . "&code=" . $code;
          
          $response = file_get_contents($token_url);
          $params = null;
          parse_str($response, $params);
          $access_token = $params['access_token'];

         // Show photo upload form to user and post to the Graph URL
         $graph_url= "https://graph.facebook.com/me/photos?"
         . "access_token=" .$access_token;


         echo '<html><body>';
         echo '<form enctype="multipart/form-data" action="'
         .$graph_url .' "method="POST">';
         echo 'Please choose a photo: ';
         echo '<input name="source" type="file"  ><br/><br/>';
         echo 'Say something about this photo: ';
         echo '<input name="message"
             type="text" value=""><br/><br/>';
         echo '<input type="submit" value="Upload"/><br/>';
         echo '</form>';
         echo '</body></html>';
        
       
      }
?>


this will output something like this

{
   "id": "517839871583123",
   "post_id": "100000713303022_517839881583112"
}

Friday, January 4, 2013

Customize Google +1 button

If you don't have code given by the google plus yet,
get it here
You will get the code like this from them

<!-- Place this tag where you want the +1 button to render. -->
<div class="g-plusone" data-annotation="inline" data-width="300"></div>



<!-- Place this tag after the last +1 button tag. -->
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>





To customize the  button...
#1 CSS
 

.Wrapper {
display:block;
padding:0px;
margin:0px;
text-decoration:none;
border:none;
width:32px; /*adjust to the width of your icon*/
height:32px; /*adjust to the height of your icon*/
overflow:hidden;
background:url(images/customize_image.png) left top no-repeat; /*Edit to match your custom icon. If you prefer to use an <img> in the div you should remove this line.*/
}
.Wrapper div {
padding:4px 0px !important; /*the original button would have a height of 24px. Add padding to the top and bottom to align it vertically. If your custom icon is 24px tall or less you dont need this line.*/
}
.Wrapper {
!display:none; /*IE7 hack. If one day google decides to support IE7 you can remove this. I dont think you want to display something that doesnt work.*/
}
.Wrapper iframe {
opacity:0;
filter:alpha(opacity=0);
zoom:1;
}
  
#2 HTML
<div class="Wrapper">
<!-- google code goes here -->
</div>

*********************************************************************
This is the complete code.
Simply copy and paste it to the HTML or even PHP page. 
<style>

.Wrapper {
display:block;
padding:0px;
margin:0px;
text-decoration:none;
border:none;
width:32px; /*adjust to the width of your icon*/
height:32px; /*adjust to the height of your icon*/
overflow:hidden;
background:url(images/customize_googleplus.png) left top no-repeat; /*Edit to match your custom icon. If you prefer to use an <img> in the div you should remove this line.*/
}
.Wrapper div {
padding:4px 0px !important; /*the original button would have a height of 24px. Add padding to the top and bottom to align it vertically. If your custom icon is 24px tall or less you dont need this line.*/
}
.Wrapper {
!display:none; /*IE7 hack. If one day google decides to support IE7 you can remove this. I dont think you want to display something that doesnt work.*/
}
.Wrapper iframe {
opacity:0;
filter:alpha(opacity=0);
zoom:1;
}

</style>


<!-- Place this tag after the last +1 button tag. -->
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>

<div class="Wrapper">
    <!-- Place this tag where you want the +1 button to render. -->        <div class="g-plusone" data-annotation="inline" data-width="300"></div>
</div>

Thursday, January 3, 2013

PHP - Problem in saving the image using imagepng

Probably you are familiar with the code below.
I am assuming that you successfully prints the image 
and only having a problem in saving it.
 
 
This code will do the solution!
Tested and working! 



header( "Content-type: image/png" ); // this will print your image imagepng($img); // set where you want your image to be saved // take note that you have to save the image // into another directory for this to work. $filename = "any_name"; $directory = "folder/".$filename.".png"; // set the directory with 0755 permission chmod($directory,0755); // this will save your image imagepng($img, $directory, 0, NULL); imagedestroy($img);