php date modify,PHP中使用date_modify作为DateTime对象的当月第一天

需要PHP 5.3才能工作(PHP 5.3中引入了“第一天”)。否则,上面的示例是唯一的方法:

// First day of this month

$d = new DateTime('first day of this month');

echo $d->format('jS, F Y');

// First day of a specific month

$d = new DateTime('2010-01-19');

$d->modify('first day of this month');

echo $d->format('jS, F Y');

// alternatively...

echo date_create('2010-01-19')

->modify('first day of this month')

->format('jS, F Y');

在PHP 5.4+中,您可以执行以下操作:

// First day of this month

echo (new DateTime('first day of this month'))->format('jS, F Y');

echo (new DateTime('2010-01-19'))

->modify('first day of this month')

->format('jS, F Y');

如果您希望使用简洁的方式执行此操作,并且已经有年份和月份的数值,则可以使用date():

echo date('Y-m-01'); // first day of this month

echo date("$year-$month-01"); // first day of a month chosen by you

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值