2012-06-28 69 views
22

我想獲得該月的第一天時間戳00:00:00。如何獲得當月的時間戳,一個月的第一天,零時

例如,揚01 2012 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; 

這回零時月份的當前日期的,而不是本月開始。

有什麼建議嗎?

回答

21

試試這個:

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

這將輸出:

June 1st 2012 12:00:00 AM 
+0

哇,我不知道 '第一天' 的存在。大!謝謝。 – JROB

+0

@JohnRobinson - 不客氣!它實際上記錄了所支持相對時間列表](http://us2.php.net/manual/en/datetime.formats.relative.php)上。如果我的帖子回答了你的問題,請考慮接受它。 – nickb

+4

你並不需要使用日期(「F Y」),你可以使用'的strtotime(「這個月的第一天」)'。你甚至可以添加另一個時間的時間戳作爲第二個參數來獲取它的第一個月的第一天。 – gwk

14

試試這個

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

翻譯爲:

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

閱讀PHP手冊上的date()time()功能。

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

此輸出:

2017-05-01 00:00:00 
相關問題