Add number of days to a date

2018-12-31 22:11发布

I want to add number of days to current date: I am using following code:

$i=30;
echo $date = strtotime(date("Y-m-d", strtotime($date)) . " +".$i."days");

But instead of getting proper date i am getting this: 2592000

Please suggest.

标签: php date
19条回答
像晚风撩人
2楼-- · 2018-12-31 22:24

You can use strtotime()
$data['created'] = date('Y-m-d H:m:s', strtotime('+1 week'));

查看更多
人气声优
3楼-- · 2018-12-31 22:24

Simple and Best

echo date('Y-m-d H:i:s')."\n";
echo "<br>";
echo date('Y-m-d H:i:s', mktime(date('H'),date('i'),date('s'), date('m'),date('d')+30,date('Y')))."\n";

Try this

查看更多
听够珍惜
4楼-- · 2018-12-31 22:27

You can do it by manipulating the timecode or by using strtotime(). Here's an example using strtotime.

$data['created'] = date('Y-m-d H:i:s', strtotime("+1 week"));

查看更多
呛了眼睛熬了心
5楼-- · 2018-12-31 22:28

Keep in mind, the change of clock changes because of daylight saving time might give you some problems when only calculating the days.

Here's a little php function which takes care of that:

function add_days($date, $days) {
    $timeStamp = strtotime(date('Y-m-d',$date));
    $timeStamp+= 24 * 60 * 60 * $days;

    // ...clock change....
    if (date("I",$timeStamp) != date("I",$date)) {
        if (date("I",$date)=="1") { 
            // summer to winter, add an hour
            $timeStamp+= 60 * 60; 
        } else {
            // summer to winter, deduct an hour
            $timeStamp-= 60 * 60;           
        } // if
    } // if
    $cur_dat = mktime(0, 0, 0, 
                      date("n", $timeStamp), 
                      date("j", $timeStamp), 
                      date("Y", $timeStamp)
                     ); 
    return $cur_dat;
}
查看更多
素衣白纱
6楼-- · 2018-12-31 22:29
//add the two day

$date = "**2-4-2016**"; //stored into date to variable

echo date("d-m-Y",strtotime($date.**' +2 days'**));

//print output
**4-4-2016**
查看更多
浅入江南
7楼-- · 2018-12-31 22:34

You may try this.

$i=30;
echo  date("Y-m-d",mktime(0,0,0,date('m'),date('d')+$i,date('Y')));
查看更多
登录 后发表回答