我有一个静态日期28-04-2012 ,.我想只更新日期到达新月28日的月份。所以如果日期是28-05-2012
,那么28-06-2012
,28-07-2012
等等。如何仅更新日期的月份和年份值?
我还想更新一年到达2013年。但始终保持日期为28?有人能告诉我这将如何使用js或php来完成吗?
我有一个静态日期28-04-2012 ,.我想只更新日期到达新月28日的月份。所以如果日期是28-05-2012
,那么28-06-2012
,28-07-2012
等等。如何仅更新日期的月份和年份值?
我还想更新一年到达2013年。但始终保持日期为28?有人能告诉我这将如何使用js或php来完成吗?
$current_date = "28-04-2012";
$next_month = strtotime($current_date . "+1month");
echo date('Y-m-d', $next_month);
最简单的一招我猜。
<?php
$date = '28-04-2012';
echo date('j-m-Y', strtotime("1 month", strtotime($date))) . "\n";
?>
在这里看到:http://www.roseindia.net/tutorial/php/phpdate/php-date-add-1-month.html
$todayDate = date("Y-m-d");// current date
echo "Today: ".$todayDate."<br>";
//Add one day to today
$dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($todayDate)) . "+1 month");
echo "After adding one month: ".date('l dS \o\f F Y', $dateOneMonthAdded)."<br>";
人们建议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。
你工作过什么吗? –