2つの日付間の差分を計算する

ネットを漁ると、秒数だけの差分や日数だけの差分が良くヒットするが

今回は、日数・時間・分・秒でそれぞれの差分が必要な場面に遭遇したので内容をメモ

javascriptで実装した

もっとスマートに書けないものか

// 現在時刻
var now = Date.now();
// この記事を書き始めた時刻
var targetDate = Date.parse('2017/03/27 23:46:00');
// 2つの日付の差分(ミリ秒)
var timeSpan = targetDate - now;
// 符号
var sign = Math.sign(timeSpan);

// 差分の絶対値
timeSpan = Math.abs(timeSpan);
// 何日何時間何分何秒離れているか計算
var day    = Math.floor(timeSpan / (1000 * 60 * 60 * 24));
var hour   = Math.floor(timeSpan / (1000 * 60 * 60)) - day * 24;
var minute = Math.floor(timeSpan / (1000 * 60)) - (day * 24 + hour) * 60;
var second = Math.floor(timeSpan / 1000) - ((day * 24 + hour) * 60 + minute) * 60;

wordpressで記事IDに紐づいた情報が欲しい

wordpressで関連記事の機能を実装しているときに使った

タイトル

echo get_the_title($postID);

関数リファレンス/get the title - WordPress Codex 日本語版

投稿日時

echo get_the_time("Y.n.j", $postID);

テンプレートタグ/get the time - WordPress Codex 日本語版

カテゴリ

$cat = get_the_category($postID);
echo $cat[0]->cat_name;

テンプレートタグ/get the category - WordPress Codex 日本語版

サムネイル画像

$thumb_id = get_post_thumbnail_id($postID);
$thumb_url = wp_get_attachment_image_src( $thumb_id , 'medium' );

// 以下は表示したい箇所で
<img src="<?php echo $thumb_url[0]; ?>">

テンプレートタグ/get post thumbnail id - WordPress Codex 日本語版

関数リファレンス/wp get attachment image src - WordPress Codex 日本語版

リンク

<a href="<?php the_permalink($postID); ?>" class="clr">

テンプレートタグ/the permalink - WordPress Codex 日本語版