栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

<?php    // 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+中,您可以执行以下操作:

<?php    // 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()

<?php    echo date('Y-m-01'); // first day of this month    echo date("$year-$month-01"); // first day of a month chosen by you


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/387981.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号