Timestamps of start and end of month

后端 未结 7 1814
独厮守ぢ
独厮守ぢ 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:52

    With PHP 5.3, you can do

    $oFirst = new DateTime('first day of this month');
    $oLast  = new DateTime('last day of this month');
    $oLast->setTime(23, 59, 59);
    

    In PHP 5.2

    Note: as AllThecode pointed out in the comments below, this next example only works if you do the $oFirst portion first. If you add +1 month to new DateTime the result will jump an extra month ahead on the last day of the month (as of php 5.5.9).

    $oToday = new DateTime();
    $iTime  = mktime(0, 0, 0, $oToday->format('m'), 1, $oToday->format('Y'));
    $oFirst = new DateTime(date('r', $iTime));
    
    $oLast  = clone $oFirst;
    $oLast->modify('+1 month');
    $oLast->modify('-1 day');
    $oLast->setTime(23, 59, 59);
    
    0 讨论(0)
提交回复
热议问题