php 在PHP中增加日期的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/660501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:24:35  来源:igfitidea点击:

Simplest way to increment a date in PHP?

phpdatetimedatestrtotimedate-manipulation

提问by davr

Say I have a string coming in, "2007-02-28", what's the simplest code I could write to turn that into "2007-03-01"? Right now I'm just using strtotime(), then adding 24*60*60, then using date(), but just wondering if there is a cleaner, simpler, or more clever way of doing it.

假设我输入了一个字符串"2007-02-28",那么我可以编写的最简单的代码是"2007-03-01"什么?现在我只是使用strtotime(),然后添加24*60*60,然后使用date(),但只是想知道是否有更干净,更简单或更聪明的方法来做到这一点。

回答by ólafur Waage

A clean way isto use strtotime()

一个干净的方法使用strtotime()

$date = strtotime("+1 day", strtotime("2007-02-28"));
echo date("Y-m-d", $date);

Will give you the 2007-03-01

会给你的 2007-03-01

回答by chaos

It's cleaner and simpler to add 86400. :)

添加 86400 更简洁更简单。:)

The high-tech way is to do:

高科技的方法是:

$date = new DateTime($input_date);
$date->modify('+1 day');
echo $date->format('Y-m-d');

but that's really only remotely worthwhile if you're doing, say, a sequence of transformations on the date, rather than just finding tomorrow.

但如果你在日期上进行一系列转换,而不是仅仅寻找明天,那这真的只是有点值得。

回答by Paul Dixon

You can do the addition right inside strtotime, e.g.

您可以在 strtotime 内进行添加,例如

 $today="2007-02-28";
 $nextday=strftime("%Y-%m-%d", strtotime("$today +1 day"));

回答by Bajlo

Another way is to use function mktime(). It is very useful function...

另一种方法是使用函数 mktime()。这是一个非常有用的功能...

$date = "2007-02-28";
list($y,$m,$d)=explode('-',$date);
$date2 = Date("Y-m-d", mktime(0,0,0,$m,$d+1,$y));

but I think strtotime()is better in that situation...

但我认为strtotime()在那种情况下更好......

回答by Biswadeep Sarkar

The simplest way...

最简单的方法...

echo date('Y-m-d',strtotime("+1 day"));    //from today

OR from specified date...

或从指定日期...

echo date('Y-m-d',strtotime("+1 day", strtotime('2007-02-28')));

回答by Aominé

Hello you can try this below especially if you are french

你好,你可以在下面试试这个,特别是如果你是法国人

$date = date('l j F Y');
#increment the date
$date2 = date('l j F Y', strtotime("+7 day"));

to translate in french you can use the setlocale() function or the function below :

要翻译法语,您可以使用 setlocale() 函数或以下函数:

function fr_date($date){

    $date = explode(' ', $date);

    $date = str_replace('Monday','Lundi',$date);
    $date = str_replace('Tuesday','Mardi',$date);
    $date = str_replace('Wednesday','Mercredi',$date);
    $date = str_replace('Thursday','Jeudi',$date);
    $date = str_replace('Friday','Vendredi',$date);
    $date = str_replace('Saturday','Samedi',$date);
    $date = str_replace('Sunday','Dimanche',$date);

    $date = str_replace('January','Janvier',$date);
    $date = str_replace('February','Février',$date);
    $date = str_replace('March','Mars',$date);
    $date = str_replace('April','Avril',$date);
    $date = str_replace('May','Mai',$date);
    $date = str_replace('June','Juin',$date);
    $date = str_replace('July','Juillet',$date);
    $date = str_replace('August','Ao?t',$date);
    $date = str_replace('September','Septembre',$date);
    $date = str_replace('October','Octobre',$date);
    $date = str_replace('November','Novembre',$date);
    $date = str_replace('December','Décembre',$date);

    $date = implode(' ',$date);
    return $date;

}

回答by Eatsleeprace Livelife

$your_date = strtotime("1month", strtotime(date("Y-m-d")));
 $new_date = date("Y-m-d", $your_date++);

回答by radhason power

 $early_start_date = date2sql($_POST['early_leave_date']);


        $date = new DateTime($early_start_date);
        $date->modify('+1 day');


        $date_a = new DateTime($early_start_date . ' ' . $_POST['start_hr'] . ':' . $_POST['start_mm']);
        $date_b = new DateTime($date->format('Y-m-d') . ' ' . $_POST['end_hr'] . ':' . $_POST['end_mm']);

        $interval = date_diff($date_a, $date_b);