获取php中给定月份和年份的开始和结束unix时间戳

dot*_*tty 9 php unix timestamp

我想找一个月的第一天(比如2010年9月1日和0:00)和一个月的最后一天(比如9月31日23:59)的时间戳.

Mat*_*iva 18

如果您将这些日期作为字符串,则只需使用strtotime(),如果您只有部分信息,则可以使用mktime().

但是,9月只有30天;)

例:

$month = 9;
$year  = 2010;

$first = mktime(0,0,0,$month,1,$year);
echo date('r', $first);

$last = mktime(23,59,00,$month+1,0,$year);
echo date('r', $last);
Run Code Online (Sandbox Code Playgroud)


edo*_*ian 5

如果您使用 PHP 5.3(不要尝试使用 5.2,日期解析器的工作方式有所不同),您可以这样说:

<?php

$date = "2010-05-10 00:00:00";

$x = new DateTime($date);

$x->modify("last day of this month");
$x->modify("last second");

echo $x->format("Y-m-d H:i:s");
// 2010-05-30 23:59:59
$timestamp = $x->getTimestamp();
Run Code Online (Sandbox Code Playgroud)