-1

(これが属するコードレビューに移動しました)。

その月の第1金曜日(午後7時)に定期的なイベントがあり、次の第1金曜日の日付を出力する必要があります。

これは私が書いたPHPの最初のビットであり、これを実装するためのより良い方法があるかどうか疑問に思っていました。サーバーはPHP5.3を実行しています

これが私が書いたものです:

$d = strtotime('today');
$t = strtotime('first friday of this month');

if ($d > $t) {
  $ff = strtotime('first friday of next month');
  $ffn = date('M j', $ff);
  echo 'Friday, '.$ffn.' at 7pm';
} elseif ($d == $t) {
  echo 'Tonight at 7pm';
} else {
  $ff = strtotime('first friday of this month');
  $fft = date('M j', $ff);
  echo 'Friday, '.$fft.' at 7pm';
}
4

1 に答える 1

2

動作確認済み

$today  = new DateTime();
$this_months_friday = new DateTime('first friday of this month');
$next_months_friday = new DateTime('first friday of next month');
echo ($today < $this_months_friday) ? $this_months_friday->format('M j') : $next_months_friday->format('M j');
于 2013-01-23T21:55:39.420 に答える