2014-08-27 47 views
0

我堅持什麼可能是一個簡單的問題,但我無法弄清楚。如何覆蓋日期到月的第一月php strtotime和日期

什麼,我試圖做的是採取說今天的日期2014年8月27日 然後將其更改爲每月的第一天所以這將是2014-08-01

我知道我可以解析它手動和做它,但似乎矯枉過正。我不熟悉與日期工作,但我嘗試的4個例子似乎沒有工作。我想將其轉換爲劃時代的時間,一旦我得到新的日期

$todays_date = date('Y-m-d'); //current date 2014-08-27 

我4次徒勞的嘗試

echo "test1: " strtotime(date('Y-m-d', $todays_date)) . "<br />"; // returns -3600 
echo "test2: " strtotime('Y-m-d', $todays_date) . "<br />"; // returns nothing 
echo "test3: " strtotime('Y-m-01', $todays_date) . "<br />"; // returns nothing 
echo "test4: " date('Y-m-01', $todays_date) . "<br />"; // returns nothing 

回答

5

相對時間格式,使這個容易:

echo (new DateTime('first day of this month'))->format('Y-m-d'); 

echo date('Y-m-d', strtotime('first day of this month')); 
+0

艾姆什麼? http://3v4l.org/nApNL – hek2mgl 2014-08-27 12:59:22

+0

@ hek2mgl''應該是'this'。經典疑難雜症。 – 2014-08-27 13:00:11

+0

啊,真好! :) ....(關於'gotcha',我剛剛複製了你的*第一個*版本;) – hek2mgl 2014-08-27 13:01:23

相關問題