我有一个日期字段一个数据库表startBefore中('Y-m-d')的格式.还有3个其他int字段表示年,月和日.我想做的是以格式填写这三个字段的新startAfter日期'Y-m-d'.
例如,我有'2001-11-14'作为startBefore日期,我想减去3年,7个月,1天来得到startAfter日期.
我有什么想法可以做到这一点?
谢谢!
Nam*_*mbi 24
使用 strtotime('date -years -months -days')
<?php
$time = strtotime('2001-11-14 -3 years -7 months -5 days');
echo $date = date("Y-m-d", $time);
Run Code Online (Sandbox Code Playgroud)
tom*_*ans 20
这是DateTime:
$start_date = '2013-03-06';
$date = DateTime::createFromFormat('Y-m-d',$start_date);
$date->modify('+1 month');
echo $date->format('Y-m-d');//2013-04-06
$date->modify('+4 year');
echo $date->format('Y-m-d');//2017-04-06
$date->modify('+6 day');
echo $date->format('Y-m-d');//2017-04-12
$date->modify('+24 hours');
echo $date->format('Y-m-d');//2017-04-13
$date->modify('-7 years');
echo $date->format('Y-m-d'); //2010-04-13
$date->modify('-18 months');
echo $date->format('Y-m-d'); //2008-10-13
Run Code Online (Sandbox Code Playgroud)
等等等等.