In my PHP code I have a date in my variable "$postedDate".
Now I want to get the date after 7 days, 15 days, one month and 2 months have elapsed.
在PHP代码中,在变量“$postedDate”中有一个日期。现在我想要7天,15天,1个月2个月后的日期。
Which date function should I use?
我应该使用哪个日期函数?
Output date format should be in US format.
输出日期格式应该是US格式。
6 个解决方案
#1
34
Use strtotime.
使用strtotime。
$newDate = strtotime('+15 days',$date)
$newDate will now be 15 days after $date. $date is unix time.
$newDate现在是$date之后的15天。日期是unix时间美元。
http://uk.php.net/strtotime
#2
17
try this
试试这个
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
#3
13
Since PHP 5.2.0 the DateTime
build in class is available
由于PHP 5.2.0,在类中构建的DateTime是可用的
$date = new DateTime($postedDate);
$date->modify('+1 day');
echo $date->format('Y-m-d');
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/class.datetime.php
#4
9
$date=strtotime(date('Y-m-d')); // if today :2013-05-23
$newDate = date('Y-m-d',strtotime('+15 days',$date));
echo $newDate; //after15 days :2013-06-07
$newDate = date('Y-m-d',strtotime('+1 month',$date));
echo $newDate; // after 1 month :2013-06-23
#5
3
This is very simple; try this:
这是很简单的;试试这个:
$date = "2013-06-12"; // date you want to upgade
echo $date = date("Y-m-d", strtotime($date ." +1 day") );
#6
2
What’s the input format anyway?
输入格式是什么?
1) If your date is, say, array of year, month and day, then you can mktime (0, 0, 0, $month, $day + 15, $year) or mktime (0, 0, 0, $month + 1, $day, $year). Note that mktime is a smart function, that will handle out-of-bounds values properly, so mktime (0, 0, 0, 13, 33, 2008) (which is month 13, day 33 of 2008) will return timestamp for February, 2, 2009.
1)如果您的日期是,比方说,年、月和日的数组,那么您可以创建mktime (0,0,0,0,0,0,0, $month, $day + 15, $year)或mktime (0,0,0,0, $month + 1, $day, $year)。请注意,mktime是一个智能函数,它将正确地处理出界值,因此mktime(0,0,0,0,13,33, 2008)(即2008年第13个月,第33天)将返回2009年2月2日的时间戳。
2) If your date is a timestamp, then you just add, like, 15*SECONDS_IN_A_DAY, and then output that with date (/* any format */, $postedDate). If you need to add one month 30 days won’t of course always work right, so you can first convert timestamp to month, day and year (with date () function) and then use (1).
2)如果您的日期是一个时间戳,那么您只需添加15*SECONDS_IN_A_DAY,然后输出日期(/*任何格式*/,$postedDate)。如果您需要添加一个月30天,当然不会总是正确的,所以您可以先将时间戳转换为月、日和年(使用date()函数),然后使用(1)。
3) If your date is a string, you first parse it, for example, with strtotime (), then do whatevee you like.
3)如果您的日期是一个字符串,您首先使用strtotime()解析它,然后执行您喜欢的操作。
#1
34
Use strtotime.
使用strtotime。
$newDate = strtotime('+15 days',$date)
$newDate will now be 15 days after $date. $date is unix time.
$newDate现在是$date之后的15天。日期是unix时间美元。
http://uk.php.net/strtotime
#2
17
try this
试试这个
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
#3
13
Since PHP 5.2.0 the DateTime
build in class is available
由于PHP 5.2.0,在类中构建的DateTime是可用的
$date = new DateTime($postedDate);
$date->modify('+1 day');
echo $date->format('Y-m-d');
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/class.datetime.php
#4
9
$date=strtotime(date('Y-m-d')); // if today :2013-05-23
$newDate = date('Y-m-d',strtotime('+15 days',$date));
echo $newDate; //after15 days :2013-06-07
$newDate = date('Y-m-d',strtotime('+1 month',$date));
echo $newDate; // after 1 month :2013-06-23
#5
3
This is very simple; try this:
这是很简单的;试试这个:
$date = "2013-06-12"; // date you want to upgade
echo $date = date("Y-m-d", strtotime($date ." +1 day") );
#6
2
What’s the input format anyway?
输入格式是什么?
1) If your date is, say, array of year, month and day, then you can mktime (0, 0, 0, $month, $day + 15, $year) or mktime (0, 0, 0, $month + 1, $day, $year). Note that mktime is a smart function, that will handle out-of-bounds values properly, so mktime (0, 0, 0, 13, 33, 2008) (which is month 13, day 33 of 2008) will return timestamp for February, 2, 2009.
1)如果您的日期是,比方说,年、月和日的数组,那么您可以创建mktime (0,0,0,0,0,0,0, $month, $day + 15, $year)或mktime (0,0,0,0, $month + 1, $day, $year)。请注意,mktime是一个智能函数,它将正确地处理出界值,因此mktime(0,0,0,0,13,33, 2008)(即2008年第13个月,第33天)将返回2009年2月2日的时间戳。
2) If your date is a timestamp, then you just add, like, 15*SECONDS_IN_A_DAY, and then output that with date (/* any format */, $postedDate). If you need to add one month 30 days won’t of course always work right, so you can first convert timestamp to month, day and year (with date () function) and then use (1).
2)如果您的日期是一个时间戳,那么您只需添加15*SECONDS_IN_A_DAY,然后输出日期(/*任何格式*/,$postedDate)。如果您需要添加一个月30天,当然不会总是正确的,所以您可以先将时间戳转换为月、日和年(使用date()函数),然后使用(1)。
3) If your date is a string, you first parse it, for example, with strtotime (), then do whatevee you like.
3)如果您的日期是一个字符串,您首先使用strtotime()解析它,然后执行您喜欢的操作。