Skip to content

Instantly share code, notes, and snippets.

@ndxbn
Last active February 8, 2021 10:53
Show Gist options
  • Save ndxbn/cf9e6f8797c59d1cfa31526480da57e8 to your computer and use it in GitHub Desktop.
Save ndxbn/cf9e6f8797c59d1cfa31526480da57e8 to your computer and use it in GitHub Desktop.
毎週火曜日に記事投稿をしたい
<?php
$baseDir = dirname(__DIR__);
$postsDir = realpath($baseDir."/_posts");
$posts = scandir($postsDir, SCANDIR_SORT_DESCENDING );
$lastPostDateString = substr($posts[1], 0, 10);
/** posts の中で一番新しいの日付のやつ */
$lastPost = new DateTimeImmutable($lastPostDateString);
/** 直近の火曜日 */
$today = new DateTimeImmutable();
$fileNameSuffix = "-0.md"; // "-0" は記事タイトルで、 Jekill の post の制限的なやつ。".md" は拡張子
foreach (new DatePeriod($lastPost, new DateInterval("P1W"), $today) as $postDate) {
$postDateString = $postDate->format("Y-m-d") . $fileNameSuffix;
$newFilePath = $postsDir."/".$postDateString;
if (file_exists($newFilePath)) {
continue;
}
// var_dump([$postDateString, $newFilePath]);
file_put_contents($newFilePath, file_get_contents("./template.md"));
}

このファイルはテンプレートです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment