无法将生日转换为字符串facebook php sdk

时间:2015-03-12 10:14:19

标签: php facebook object facebook-php-sdk

我正在测试facebook php sdk。 我拥有'user_birthday,email'和默认用户个人资料的权限。 这是代码。

<?php
if(isset($session)) {

  try {

$user_profile = (new FacebookRequest(
  $session, 'GET', '/me/'
))->execute()->getGraphObject(GraphUser::className());

   var_dump ($user_profile);
   echo $user_profile->getEmail();
   $_SESSION['user']=$user_profile->getName();
   $_SESSION['user_id']=$user_profile->getId();

    $_SESSION['profile_pic']="http://graph.facebook.com/".$_SESSION['user_id']."/picture";
   $_SESSION['gender']=$user_profile->getGender();

 $vars = get_object_vars ( $user_profile->getBirthday() );


 print_r ( $vars );


 } catch(FacebookRequestException $e) {

echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();

  }   

}        
?>

输出

    object(Facebook\GraphUser)[6]
    protected 'backingData' => 
    array (size=12)
    'id' => string 'xxxxxxxxxxx' (length=15)
    'birthday' => string '01/12/1990' (length=10)
    'email' => string 'xxxxx@gmail.com' (length=19)
    'first_name' => string 'xxxx' (length=10)
    'gender' => string 'male' (length=4)
    'last_name' => string 'xxxx' (length=4)
    'link' => string 'https://www.facebook.com/app_scoped_user_id/xxxxxx/' (length=60)
    'locale' => string 'en_US' (length=5)
    'name' => string 'xxxxx xxxxx' (length=15)
    'timezone' => int -8
    'updated_time' => string '2015-02-22T07:48:23+0000' (length=24)
    'verified' => boolean true

     xxxxx@gmail.comArray ( [date] => 1993-04-22 00:00:00 [timezone_type] => 3 [timezone] => UTC ) 
问题是,我不能使用生日对象。 当我用回声检查生日时,

    echo $user_profile->getBirthday();

我收到了这个错误。

     Catchable fatal error: Object of class DateTime could not be converted to string

当我转储$ user_profile-&gt; getBirthday(); 输出是。

      object(DateTime)[9]
      public 'date' => string '1993-04-22 00:00:00' (length=19)
      public 'timezone_type' => int 3
      public 'timezone' => string 'UTC' (length=3)

如何将生日作为字符串?  我是php的新手。  任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:5)

使用DateTime对象的format方法:

$user_profile->getBirthday()->format('m/d/Y');

有关构建首选格式字符串的帮助,请参阅this page