【问题标题】:strftime/strtotime syntaxstrftime/strtotime 语法
【发布时间】:2013-01-16 23:46:45
【问题描述】:

我有这段工作代码 (PHP)。我遇到的问题是 strftime('%e %B',strtotime($time));部分(位于块的末尾)。它什么也没返回..我做错了什么?可能是语法错误?

<?php
function buildBaseString($baseURI, $method, $params)
{
    $r = array();
    ksort($params);
    foreach($params as $key=>$value){
        $r[] = "$key=" . rawurlencode($value);
    }

    return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r)); //return complete base string
}

function buildAuthorizationHeader($oauth)
{
    $r = 'Authorization: OAuth ';
    $values = array();
    foreach($oauth as $key=>$value)
        $values[] = "$key=\"" . rawurlencode($value) . "\"";

    $r .= implode(', ', $values);
    return $r;
}

$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";

$oauth_access_token = "XXXXXX";
$oauth_access_token_secret = "XXXXXX";
$consumer_key = "XXXXXX";
$consumer_secret = "XXXXXX";

$oauth = array( 'oauth_consumer_key' => $consumer_key,
    'oauth_nonce' => time(),
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_token' => $oauth_access_token,
    'oauth_timestamp' => time(),
    'count' => 6,   
    'oauth_version' => '1.0');

$base_info = buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;


$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
    CURLOPT_HEADER => false,
    CURLOPT_URL => $url . '?count=6',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false);

$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

$twitter_data = json_decode($json);

echo "
            <ul style='color:#6E6E6E'>";

foreach ($twitter_data as $tweet)
{
    if (!empty($tweet)) {
        $text = $tweet->text;
    }
    if (!empty($tweet)) {
        $id = $tweet->id;
    }
    if (!empty($tweet)) {
        $time = $tweet->created_at;
    }
    if (!empty($tweet)) {
        $username = $tweet->user->name;
    }
    echo '<li>';
    echo $text . "<br><a href=\"http://twitter.com/";
    echo $username ;
    echo '/status/';
    echo $id ;
    echo '"><small>';
    strftime('%e %B',strtotime($time));
    ' </small></a> - <a href="http://twitter.com/intent/tweet?in_reply_to=';
    echo $id;
    echo '"><small>rispondi</small></a> - <a href="http://twitter.com/intent/retweet?tweet_id=';
    echo $id;
    echo '"><small>retweet</small></a> - <a href="http://twitter.com/intent/favorite?tweet_id=';
    echo $id;
    echo '"><small>preferito</small></a></li>';
}

echo '
            </ul>';

?>

【问题讨论】:

  • 下次你能尝试发布一个更简单的例子吗?如果周围没有其他所有东西,调试起来会容易一些。只需检查它是否在没有所有推特内容的情况下仍然失败,然后发布 1 条或 2 行您可以验证的内容是否也没有按预期工作?
  • 嗯,需要 twitter 部分,因为我需要确保该函数在 twitter JSON 数据检索到的时间上工作。

标签: php json syntax strtotime strftime


【解决方案1】:

只是我还是您只是缺少echo?还是您打算与. 连接?

...
echo $id ;
echo '"><small>'; 
strftime('%e %B',strtotime($time)); ' </small></a> - <a href="http://twitter.com/intent/tweet?in_reply_to='; 
echo $id;
...

应该是……

...
echo $id ;
echo '"><small>'; 
echo strftime('%e %B',strtotime($time));
echo ' </small></a> - <a href="http://twitter.com/intent/tweet?in_reply_to='; 
echo $id; 
...

【讨论】:

  • 天啊..我该休息了。啊啊谢谢! :)
  • @MultiformeIngegno 第二双眼睛通常会有所帮助。
【解决方案2】:

警告

仅限 Windows:

此函数的 Windows 实现不支持 %e 修饰符。要实现此值,可以改用 %#d 修饰符。下面的例子说明了如何编写一个跨平台兼容的函数。

%z 和 %Z 修饰符都返回时区名称,而不是偏移量或缩写。

参考:

【讨论】:

  • 既然你在你的帖子中没有提到这一点,我无法知道:)
  • 你是对的。 :) 无论如何,我认为这不是函数的问题.. 更多的是语法问题.. 因为在同一个 VPS 上,我以相同的方式使用该函数(对于其他东西)并且它可以工作
  • 你没有检查$date是否被设置,你能重新检查它是否被设置并且有真实数据吗?
  • 我会在这里留下我的答案,因为它可能对其他用户有帮助。
猜你喜欢
  • 2015-08-17
  • 2013-01-08
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
  • 2017-01-15
  • 2022-01-13
相关资源
最近更新 更多