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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11236708/
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 00:02:30  来源:igfitidea点击:

How to get timestamp of current month, first day of month, zero hour

phpdatetimestampstrtotime

提问by JROB

I am trying to get the timestamp of the first day of the month at 00:00:00.

我试图在 00:00:00 获取当月第一天的时间戳。

For example, the timestamp of Jan 01 2012 00:00:00

例如,时间戳为 Jan 01 2012 00:00:00

I'm doing this:

我这样做:

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

This is returning zero hour of the current day of the month, not the start of the month.

这是返回当月当天的零小时,而不是本月的开始。

Any suggestions?

有什么建议?

采纳答案by nickb

Try this:

尝试这个:

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

This will output:

这将输出:

June 1st 2012 12:00:00 AM

回答by sesser

Try this

尝试这个

$time = strtotime(date('Y-m-01 00:00:00')); // == 1338534000

Which translates to:

翻译成:

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

Read the PHP manual on the date()and time()functions.

阅读有关date()time()函数的 PHP 手册。

回答by Goldbug

echo date("Y-m-d 00:00:00", strtotime("first day of this month"));

This outputs:

这输出:

2017-05-01 00:00:00