2012-05-08 47 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。

相關問題