欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

已知日期:2013-07-31 加12个月 3个月 1个月 得到相应的日期

程序员文章站 2023-12-29 19:59:28
...
已知日期:2013-07-31 加12个月 3个月 1个月 得到相应的日期

求大牛怎么写PHP代码?

回复讨论(解决方案)


$date1="2006-08-09";
echo date('Y-m-d',strtotime("$date1 +5 day")); //相应地,要增加月,年,将day改成month或year即可
?>
输出结果:2006-08-14

+++++++++++++ 今天、昨天、明天 、上一周、下一周 +++++++++++++++

date_default_timezone_set('PRC'); //默认时区
echo "今天:",date("Y-m-d",time()),"
";
echo "昨天:",date("Y-m-d",strtotime("-1 day")), "
";
echo "明天:",date("Y-m-d",strtotime("+1 day")), "
";
echo "一周后:",date("Y-m-d",strtotime("+1 week")), "
";
echo "一周零两天四小时两秒后:",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "
";
echo "下个星期四:",date("Y-m-d",strtotime("next Thursday")), "
";
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."
";
echo "一个月前:".date("Y-m-d",strtotime("last month"))."
";
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."
";
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."
";

?>


$date1="2006-08-09";
echo date('Y-m-d',strtotime("$date1 +5 day")); //相应地,要增加月,年,将day改成month或year即可
?>
输出结果:2006-08-14

+++++++++++++ 今天、昨天、明天 、上一周、下一周 +++++++++++++++

date_default_timezone_set('PRC'); //默认时区
echo "今天:",date("Y-m-d",time()),"
";
echo "昨天:",date("Y-m-d",strtotime("-1 day")), "
";
echo "明天:",date("Y-m-d",strtotime("+1 day")), "
";
echo "一周后:",date("Y-m-d",strtotime("+1 week")), "
";
echo "一周零两天四小时两秒后:",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "
";
echo "下个星期四:",date("Y-m-d",strtotime("next Thursday")), "
";
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."
";
echo "一个月前:".date("Y-m-d",strtotime("last month"))."
";
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."
";
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."
";

?>
这个不行。要加自然月,有的月分28天。有的31天

$s='2013-07-31';
echo date("Y-m-d",strtotime("$s+1 month"));

呵呵,想当然了

echo date("Y-m-d\n",strtotime("$s+7 month"));
得到 2014-03-03
select adddate('2013-07-31', INTERVAL 7 month)
得到 2014-02-28

显然,php 的表现是不尽人意的

mktime其实也可以达到要求

$t = '2013-07-31';echo date("Y-m-d",mktime(0,0,0,date("m") + 12,date("d") , date("Y"))).'
';echo date("Y-m-d",mktime(0,0,0,date("m") + 3,date("d") , date("Y"))).'
';echo date("Y-m-d",mktime(0,0,0,date("m") + 1,date("d") , date("Y"))).'
';



结果:
2014-07-312013-10-312013-08-31

$t = '2013-07-31';echo date("Y-m-d",mktime(0,0,0,date("m") + 2,date("d") , date("Y")));

得 2013-10-01

最要不得的是:回避矛盾

上一篇:

下一篇: