2012-05-08 48 views
-1

我有一个静态日期28-04-2012 ,.我想只更新日期到达新月28日的月份。所以如果日期是28-05-2012,那么28-06-2012,28-07-2012等等。如何仅更新日期的月份和年份值?

我还想更新一年到达2013年。但始终保持日期为28?有人能告诉我这将如何使用js或php来完成吗?

+0

你工作过什么吗? –

回答

2
$current_date = "28-04-2012"; 
$next_month = strtotime($current_date . "+1month"); 

echo date('Y-m-d', $next_month); 

最简单的一招我猜。

2
<?php 
$date = '28-04-2012'; 

echo date('j-m-Y', strtotime("1 month", strtotime($date))) . "\n"; 
?> 
0

人们建议strtotime(),但我不得不说我对这个功能很不喜欢。它不是为日期算术设计的,但它是老版本PHP中唯一可用的选项,所以每个人都使用它。

更好的解决方案可以在PHP5 DateTime class中找到,特别是DateTime::Add方法(也称为date_add()函数)。

使用的唯一原因strtotime() instead of DateTime :: Add()is if you're using a version of PHP older than 5.3, since this is when the Add`方法被添加。但是,如果是这样的话,你的首要任务应该是升级你的PHP。

相关问题