我创建了这个页面http://www.islandstreet.co.uk/events/,它使用 Facebook API 从 Facebook 检索我的活动详细信息并将它们显示在我的网站上。
我需要解决的问题是日期显示为 UTC 时间戳。我似乎无法将它们转换为人类可读的格式,例如“2013 年 2 月 21 日星期四晚上 7 点”。
我并不完全了解 PHP,并且一直在使用教程来做到这一点。
这是我正在使用的代码;
<?php
//we have to set timezone to California
date_default_timezone_set('Europe/London');
//requiring FB PHP SDK
require 'facebook.php';
//initializing keys
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true, // enable optional cookie support
));
//just a heading
echo "";
echo "";
echo "";
echo "";
//query the events
//we will select name, start_time, end_time, location, description this time
//but there are other data that you can get on the event table (https://developers.facebook.com/docs/reference/fql/event/)
//as you've noticed, we have TWO select statement here
//since we can't just do "WHERE creator = your_fan_page_id".
//only eid is indexable in the event table, sow we have to retrieve
//list of events by eids
//and this was achieved by selecting all eid from
//event_member table where the uid is the id of your fanpage.
//*yes, you fanpage automatically becomes an event_member
//once it creates an event
$fql = "SELECT name, pic, start_time, end_time, location, description
FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = 163579547024154 )
ORDER BY start_time asc";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
//looping through retrieved data
foreach( $fqlResult as $keys => $values ){
//see here http://php.net/manual/en/function.date.php for the date format I used
//The pattern string I used 'l, F d, Y g:i a'
//will output something like this: July 30, 2015 6:30 pm
//getting 'start' and 'end' date,
//'l, F d, Y' pattern string will give us
//something like: Thursday, July 30, 2015
$start_date = date($values['start_time']);
$end_date = date($values['end_time']);
//getting 'start' and 'end' time
//'g:i a' will give us something
//like 6:30 pm
//printing the data
echo "<div class='event'>";
echo "<div style='float: left; margin: 0 8px 0 0;'>";
echo "<img src={$values['pic']} />";
echo "</div>";
echo "<div style='float: left;'>";
echo "<div style='font-size: 26px'>{$values['name']}</div>";
if( $start_date == $end_date ){
//if $start_date and $end_date is the same
//it means the event will happen on the same day
//so we will have a format something like:
//July 30, 2015 - 6:30 pm to 9:30 pm
echo "<div>{$start_date}</div><div>{$start_time}</div>";
}else{
//else if $start_date and $end_date is NOT the equal
//it means that the event will will be
//extended to another day
//so we will have a format something like:
//July 30, 2013 9:00 pm to Wednesday, July 31, 2013 at 1:00 am
echo "<div>on {$start_date}</div><div> {$start_time}</div>";
}
echo "<div>Location: " . $values['location'] . "</div>";
echo "<div>More Info: " . $values['description'] . "</div>";
echo "</div>";
echo "<div style='clear: both'></div>";
echo "</div>";
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js">
</script>
<script type='text/javascript'>
//just to add some hover effects
$(document).ready(function(){
$('.event').hover(
function () {
$(this).css('background-color', '#CFF');
},
function () {
$(this).css('background-color', '#E3E3E3');
}
);
});
</script>