The first day of the current month in php using da

2019-01-03 05:13发布

I can get Monday this week with:

$monday = date_create()->modify('this Monday');

I would like to get with the same ease the 1st of this month. How can I achieve that?

Thanks

9条回答
2楼-- · 2019-01-03 05:36

Currently I'm using this solution:

$firstDay = new \DateTime('first day of this month');
$lastDay = new \DateTime('last day of this month');

The only issue I came upon is that strange time is being set. I needed correct range for our search interface and I ended up with this:

$firstDay = new \DateTime('first day of this month 00:00:00');
$lastDay = new \DateTime('first day of next month 00:00:00');
查看更多
放荡不羁爱自由
3楼-- · 2019-01-03 05:38

using date method, we should be able to get the result. ie; date('N/D/l', mktime(0, 0, 0, month, day, year));

For Example

echo date('N', mktime(0, 0, 0, 7, 1, 2017));   // will return 6
echo date('D', mktime(0, 0, 0, 7, 1, 2017));   // will return Sat
echo date('l', mktime(0, 0, 0, 7, 1, 2017));   // will return Saturday
查看更多
不美不萌又怎样
4楼-- · 2019-01-03 05:41

Ugly, (and doesn't use your method call above) but works:

echo 'First day of the month: ' . date('m/d/y h:i a',(strtotime('this month',strtotime(date('m/01/y')))));   
查看更多
疯言疯语
5楼-- · 2019-01-03 05:42

You can do it like this:

$firstday = date_create()->modify('first day January 2010');
查看更多
我想做一个坏孩纸
6楼-- · 2019-01-03 05:43

Here is what I use.

First day of the month:

date('Y-m-01');

Last day of the month:

date('Y-m-t');
查看更多
欢心
7楼-- · 2019-01-03 05:49

I use a crazy way to do this is using this command

$firstDay=date('Y-m-d',strtotime("first day of this month"));
$lastDay=date('Y-m-d',strtotime("last day of this month"));

Thats all

查看更多
登录 后发表回答