php 月初和月底的时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4702696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:03:08  来源:igfitidea点击:

Timestamps of start and end of month

php

提问by Kir

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

如何使用 PHP 获取任何一个月的第一分钟和最后一分钟的时间戳?

回答by Felix Kling

You can use mktimeand date:

您可以使用mktimedate

$first_minute = mktime(0, 0, 0, date("n"), 1);
$last_minute = mktime(23, 59, 59, date("n"), date("t"));

That is for the current month. If you want to have it for any month, you have change the month and day parameter accordingly.

那是当月的。如果您想在任何月份使用它,您可以相应地更改月和日参数。

If you want to generate it for every month, you can just loop:

如果你想每个月都生成它,你可以循环:

$times  = array();
for($month = 1; $month <= 12; $month++) {
    $first_minute = mktime(0, 0, 0, $month, 1);
    $last_minute = mktime(23, 59, 59, $month, date('t', $first_minute));
    $times[$month] = array($first_minute, $last_minute);
}

DEMO

演示

回答by enobrev

With PHP 5.3, you can do

使用 PHP 5.3,你可以做到

$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

在 PHP 5.2 中

Note: as AllThecodepointed out in the comments below, this next example onlyworks if you do the $oFirstportion first. If you add +1 monthto new DateTimethe result will jump an extra month ahead on the last day of the month (as of php 5.5.9).

注意正如AllThecode在下面的评论中指出的那样,下一个示例在您先执行该$oFirst部分时才有效。如果添加+1 monthnew DateTime结果将在该月的最后一天提前一个月(从 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);

回答by phihag

Use mktimefor generating timestamps from hour/month/day/... values and cal_days_in_monthto get the number of days in a month:

使用mktime从小时/月/日/... 值和cal_days_in_month生成时间戳以获取一个月中的天数:

$month = 1; $year = 2011;
$firstMinute = mktime(0, 0, 0, $month, 1, $year);
$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$lastMinute = mktime(23, 59, 0, $month, $days, $year);

回答by amielcar

I think better then

我觉得更好

$first_minute = mktime(0, 0, 0, date("n"), 1);
$last_minute = mktime(23, 59, 0, date("n"), date("t"));

is:

是:

$first_minute = mktime(0, 0, 0, date("n"), 1);
$last_minute = mktime(23, 59, 0, date("n") + 1, 0);

回答by amielcar

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

这需要 PHP > 5.2 并且需要对“分钟”部分进行调整

$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

参考:http: //www.php.net/manual/en/class.datetime.php

回答by Prahlad

$date = new \DateTime('now');//Current time
$date->modify("-1 month");//get last month
$startDate = $date->format('Y-m-01');
$endDate = $date->format('Y-m-t');

回答by Rahamath Ali Khan

Best Way do like this..

最好的方式这样做..

$first_day = date('m-01-Y h:i:s',strtotime("-1 months"));

$first_day = date('m-01-Y h:i:s',strtotime("-1 个月"));

$last_day = date('m-t-Y h:i:s',strtotime("-1 months"));

$last_day = date('mtY h:i:s',strtotime("-1 个月"));