2014-08-27 72 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

相关问题