6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

年と週番号から、その週の最初の日付文字列を得る

Last updated at Posted at 2016-03-18

 年と週番号から日付を得る関数ってあるんだろうか。ない。ということでISO 8601準拠で書いた。


public function week_start_date($year, $week_number=1) {
    $time = strtotime("{$year}-01-01");
    $week = date("w", $time);
    if($week>4 || $week==0) {
        $time = strtotime("next Monday", $time);
    } elseif ($week != 1) {
        $time = strtotime("last Monday", $time);
    }
    $week_number--;
    return date("Y/m/d", strtotime("+{$week_number} Week", $time));
}

 エラー処理はしていません。関数名も気が利いてないし、ここから週の範囲も得られるようなオプションなど手を加えていこうかな、というところです。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?