如何获取当月,第一天,零小时的时间戳

JRO*_*ROB 24 php timestamp date strtotime

我想在00:00:00获得该月第一天的时间戳.

例如,2012年1月1日00:00:00的时间戳

我这样做:

$test_month = time(); // Seconds
$test_month = date('M', $test_month); // This month
$test_month = strtotime($test_month); // Timestamp of this month
echo $test_month;
Run Code Online (Sandbox Code Playgroud)

这是返回当月当天的零小时,而不是月初.

有什么建议?

nic*_*ckb 23

试试这个:

echo date( 'F jS Y h:i:s A', strtotime( 'first day of ' . date( 'F Y')));
Run Code Online (Sandbox Code Playgroud)

这将输出:

June 1st 2012 12:00:00 AM
Run Code Online (Sandbox Code Playgroud)

  • 你不需要使用日期('F Y'),你可以使用`strtotime('本月的第一天')`.您甚至可以添加另一个时间的时间戳作为第二个参数来获取它的第一天. (4认同)

ses*_*ser 14

试试这个

$time = strtotime(date('Y-m-01 00:00:00')); // == 1338534000
Run Code Online (Sandbox Code Playgroud)

这意味着:

$date = date('Y-m-d H:i:s', $time); // == 2012-06-01 00:00:00
Run Code Online (Sandbox Code Playgroud)

阅读关于date()time()函数的PHP手册.


Gol*_*bug 5

echo date("Y-m-d 00:00:00", strtotime("first day of this month"));
Run Code Online (Sandbox Code Playgroud)

这输出:

2017-05-01 00:00:00
Run Code Online (Sandbox Code Playgroud)