前提
Twitter APIと連携したlaravelのサービスを開発しています
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- twitter APIで取得したレスポンスから取り出した日付をif文に使いたい
該当のソースコード
Twitter APIから最新1件のツイートを取得してそのツイートが15日以上前の物だった場合
フラグをfalseにする処理を書いています。
テストコードでtodayの日付を固定化するためcarbonを使っています。
PHP
1 $targetTweets = $twitter->get("statuses/user_timeline", $targetParameters); 2 $latestTweetTime = ''; 3 foreach ($targetTweets as $target_tweet) { 4 $latestTweetTime = new DateTime($target_tweet->created_at); 5 // レスポンスにはグリニッジの標準時刻で登録されているため時差調整 6 $latestTweetTime = $latestTweetTime->modify('+9 hours'); 7 8 9 } 10 $today = Carbon::now(); 11 $unfollow_day = $latestTweetTime->modify('+15 days'); 12 Log::debug($today->format('Y-m-d H:i:s')); 13 Log::debug($unfollow_day->format('Y-m-d H:i:s')); 14 if ($today > $unfollow_day) { 15 $chk_flg = false; 16 }
ログに出力された日付は
$today: 2022-01-07 14:24:01
$unfollow_day:2022-01-07 13:01:31
のためtodayの方が新しい日付となりif文の中の
フラグの値変更処理が動く想定ですが動いていません。
確認する箇所はどこになるでしょうか?
試したこと
回答をいただいて$unfollow_dayもcarbon化し、gtメソッドを使うようにしてみました
PHP
1 $targetTweets = $twitter->get("statuses/user_timeline", $targetParameters); 2 $latestTweetTime = ''; 3 foreach ($targetTweets as $target_tweet) { 4 $latestTweetTime = $target_tweet->created_at; 5 6 7 } 8 $today = Carbon::now(); 9 $unfollow_day = new Carbon($latestTweetTime); 10 $unfollow_day = $unfollow_day->addDays(15); 11 $unfollow_day = $unfollow_day->addHours(9); 12 Log::debug($today->format('Y-m-d H:i:s')); 13 Log::debug($unfollow_day->format('Y-m-d H:i:s')); 14 var_dump($today->gt($unfollow_day)); 15 var_dump($today->lt($unfollow_day)); 16 if ($today->gt($unfollow_day)) { 17 Log::debug('到達確認'); 18 $chk_flg = false; 19 }
ログに出力された日付は
$today: 2022-01-07 14:24:01
$unfollow_day:2022-01-07 13:01:31
のためtodayの方が新しい日付ですが
var_dump($today->gt($unfollow_day));
はfalseを
var_dump($today->lt($unfollow_day));
はtrueを返しています
また、$todayの日付の方が古いテストパターンも試しました
$today: 2022-08-30 22:54:48
$unfollow_day:2022-09-14 01:37:00
ところがこちらも
var_dump($today->gt($unfollow_day));
はfalseを
var_dump($today->lt($unfollow_day));
はtrueを返しています
補足情報(FW/ツールのバージョンなど)
Laravel v8.83.23
PHP v7.4.18
twitter APIからのレスポンス
"created_at": "Thu Apr 06 15:28:43 +0000 2017",
を取得して比較に使用しています
JSON
1[ 2 { 3 "created_at": "Thu Apr 06 15:28:43 +0000 2017", 4 "id": 850007368138018817, 5 "id_str": "850007368138018817", 6 "text": "RT @TwitterDev: 1/ Today we’re sharing our vision for the future of the Twitter API platform!nhttps://t.co/XweGngmxlP", 7 "truncated": false, 8 "entities": { 9 "hashtags": [], 10 "symbols": [], 11 "user_mentions": [ 12 { 13 中略 14 } 15 ], 16 "urls": [ 17 { 18 中略 19 } 20 ] 21 }, 22 "source": "<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>", 23 "in_reply_to_status_id": null, 24 "in_reply_to_status_id_str": null, 25 "in_reply_to_user_id": null, 26 "in_reply_to_user_id_str": null, 27 "in_reply_to_screen_name": null, 28 "user": { 29 "id": 6253282, 30 "id_str": "6253282", 31 "name": "Twitter API", 32 "screen_name": "twitterapi", 33 "location": "San Francisco, CA", 34 "description": "The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.", 35 "url": "http://t.co/78pYTvWfJd", 36 "entities": { 37 "url": { 38 "urls": [ 39 { 40 中略 41 } 42 ] 43 }, 44 "description": { 45 "urls": [] 46 } 47 }, 48 中略 49 }, 50 "geo": null, 51 "coordinates": null, 52 "place": null, 53 "contributors": null, 54 "retweeted_status": { 55 "created_at": "Thu Apr 06 15:24:15 +0000 2017", 56 "id": 850006245121695744, 57 "id_str": "850006245121695744", 58 "text": "1/ Today we’re sharing our vision for the future of the Twitter API platform!nhttps://t.co/XweGngmxlP", 59 "truncated": false, 60 "entities": { 61 "hashtags": [], 62 "symbols": [], 63 "user_mentions": [], 64 "urls": [ 65 { 66 中略 67 } 68 ] 69 }, 70 中略 71 "entities": { 72 "url": { 73 "urls": [ 74 { 75 "url": "https://t.co/66w26cua1O", 76 "expanded_url": "https://dev.twitter.com/", 77 "display_url": "dev.twitter.com", 78 "indices": [ 79 0, 80 23 81 ] 82 } 83 ] 84 }, 85 "description": { 86 "urls": [ 87 { 88 中略 89 } 90 ] 91 } 92 }, 93 中略 94 }, 95 中略 96 }, 97 中略 98 }, 99 { 100 "created_at": "Mon Apr 03 16:09:50 +0000 2017", 101 "id": 848930551989915648, 102 "id_str": "848930551989915648", 103 "text": "RT @TwitterMktg: Starting today, businesses can request and share locations when engaging with people in Direct Messages. https://t.co/rpYn…", 104 "truncated": false, 105 "entities": { 106 "hashtags": [], 107 "symbols": [], 108 "user_mentions": [ 109 { 110 中略 111 } 112 ], 113 "urls": [] 114 }, 115 中略 116 "entities": { 117 "url": { 118 "urls": [ 119 { 120 中略 121 } 122 ] 123 }, 124 "description": { 125 "urls": [] 126 } 127 }, 128 中略 129 }, 130 中略 131 "entities": { 132 "hashtags": [], 133 "symbols": [], 134 "user_mentions": [], 135 "urls": [ 136 { 137 中略 138 } 139 ] 140 }, 141 中略 142 "entities": { 143 "url": { 144 "urls": [ 145 { 146 中略 147 } 148 ] 149 }, 150 "description": { 151 "urls": [] 152 } 153 }, 154 中略 155 }, 156 中略 157 }, 158 中略 159 } 160] 161

回答2件
あなたの回答
tips
プレビュー