1

我创建了这个页面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>
4

3 回答 3

0

date 和 mktime 是你的朋友,这里是示例http://www.anchorx.com/ali/2013/02/06/php-date-format-example/

于 2013-02-06T16:51:12.270 回答
0

在 PHP 中,该date()函数接受两个参数,格式和时间戳。

string date ( string $format [, int $timestamp = time() ] )

因此,您将使用以下代码输出星期几(星期日至星期六)、月份(1 月至 12 月)、日期(1-31)、年份(2013)、字母at小时(12小时时钟)和ampm

 $start_date = date('l, F d, Y \a\t ga', $values['start_time']);

PHP 手册 - 日期参考

于 2013-02-06T16:52:08.343 回答
0
// Format: Thursday 21st February 2013 at 7pm
date("l jS F Y \a\\t ga", $values['start_time']);

看到它在行动

于 2013-02-06T16:52:59.743 回答