如何将日期改写为每月1日php strtotime和date


how to overwrite date to 1st of month php strtotime and date

我陷入了一个可能很简单的问题,但我无法解决。

我想做的是假设今天的日期2014-08-27然后把它改为本月1日,所以它将是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

相对时间格式使此操作变得简单:

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

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