doufuhuang6981 2016-04-05 05:18
浏览 197
已采纳

如何在PHP中从datetime中减去1天16小时

I'm trying subtracting Day and Hour from given date time example:

$schedule_date_time = '2016-02-29 08:30 PM';
echo date( "Y-m-d h:i a", strtotime("-1 day 16 hours", strtotime($schedule_date_time)) );

It gives me following result: 2016-02-29 12:30 pm

Which is not correct...

But If I do this :

$schedule_date_time = '2016-02-29 08:30 PM';
    echo date( "Y-m-d h:i a", strtotime("-1 day -16 hours", strtotime($schedule_date_time)) );

It gives me following result: 2016-02-28 04:30 am

which is correct but is not possible for me to identify and put minus sign in string just before the integer.

  • 写回答

3条回答 默认 最新

  • drpfu51608120170 2016-04-05 05:24
    关注

    Use date_sub/DateTime::sub.

    $date = date_create("2016-02-29 08:30 PM") ;
    date_sub($date, date_interval_create_from_date_string('1 day 16 hours'));
    echo date_format($date, 'Y-m-d h:i a'); // 2016-02-28 04:30 am
    

    If you want add an amount of time instead of subtracts it, use date_add instead.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部