Timestamps of start and end of month

后端 未结 7 1852
独厮守ぢ
独厮守ぢ 2020-12-03 17:04

How can one get the timestamps of the first and last minutes of any month using PHP?

7条回答
  •  情话喂你
    2020-12-03 17:40

    This requires PHP > 5.2 and need adjustement for the "minutes" part

    $year = ...;  // this is your year
    $month = ...; // this is your month
    $month = ($month < 10 ? '0' . $month : $month);
    $start = new DateTime($year . '-' . $month . '-01 00:00:00');
    $end = $start->modify('+1 month -1 day -1 minute'); //perhaps this need 3 "->modify"
    echo $start->format('U');
    echo $end->format('U');
    

    (not tested)

    Ref: http://www.php.net/manual/en/class.datetime.php

提交回复
热议问题