dongyizhuang0134 2012-08-21 22:43
浏览 34
已采纳

PHP DateTime修改为月中的特定日期

I am trying to use the php DateTime objects modify function to modify he date to a specific date in the month i.e. 10th or 20th etc.

I have tried all of the following, none of which work:

$d = new DateTime();

$d->modify('10th day');
$d->modify('10th');
$d->modify('10th of this month');
$d->modify('10th of the month');
  • 写回答

3条回答 默认 最新

  • doutu3352 2012-08-21 22:57
    关注

    Assuming you don't know the year and month (if you do, use DateTime::setDate()), one way to do this would like this:

    $day = 10; // 10th of the month
    $dt = new DateTime('first day of this month');
    $dt->modify('+' . ($day - 1) . ' days');
    

    I have a feeling that there's also a way to do it strictly through the constructor, but like you, I couldn't figure out the magic string. Odd that "first day of the month" works, but "tenth day of the month" does not.

    Edit: Apparently it's impossible to do this through the constructor. Even Rasmus suggests doing it this way instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改