简体   繁体   English

如何在 PHP 中将日期增加一天?

[英]How can I increment a date by one day in Php?

I am using this this syntax to increase one day above but when i put this format it still give me wrong date like this.我正在使用这种语法来增加一天的时间,但是当我使用这种格式时,它仍然给我错误的日期。 '01/01/1970' But I want format and date like this '25/08/2016'. '01/01/1970' 但我想要这样的格式和日期'25/08/2016'。

$today = '24/08/2016';
$nextday = strftime("%d/%m/%Y", strtotime("$today +1 day"));

so please help me how can i do this.advance thanx.所以请帮助我如何做到这一点。提前thanx。

You can use strtotime .您可以使用strtotime

$your_date = strtotime("1 day", strtotime("2016-08-24"));
$new_date = date("Y-m-d", $your_date);

Hope it will help you.希望它会帮助你。

It's important to note the different interpretation of - and / in the date.重要的是要注意日期中-/的不同解释。 If you use a - php will determine it to be DD-MM , if you use a / php will determine it to be MM-DD .如果使用- php 将确定它是DD-MM ,如果使用 a / php 将确定它是MM-DD So you need to use - instead of /所以你需要使用-而不是/

<?php

$today = '24-08-2016';
echo $nextday = date("d-m-Y", strtotime("$today +1 day"));

?>

See below perfect working code请参阅下面的完美工作代码

<?php
    echo $startDate = date('Y-m-d H:i:s'); echo "<br/>";
    echo $nextDate = date("Y-m-d H:i:s", strtotime("$startDate +1 day"));
?>

If you want to use DateTime:如果要使用日期时间:

$today      = '24/08/2016';    
$nextDay    = DateTime::createFromFormat('d/m/Y', $today)
                ->add(new DateInterval('P1D'))
                ->format('d/m/Y');

You should replace the / with -您应该将 / 替换为 -

Dates in the m/d/y or dmy formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/) , then the American m/d/y is assumed; m/d/y 或 dmy 格式的日期通过查看各个组件之间的分隔符来消除歧义:如果分隔符是斜杠 (/) ,则假定为美国m/d/y whereas if the separator is a dash (-) or a dot (.) , then the European** dmy format is assumed**.而如果分隔符是破折号 (-) 或点 (.) ,则假定为欧洲** dmy格式**。

Reference: http://php.net/manual/en/function.strtotime.php参考: http : //php.net/manual/en/function.strtotime.php

Try this:试试这个:

$today = '24/08/2016';
$today = str_replace('/', '-', $today);
$today = date('Y-m-d', strtotime($today));
$nextday = date("d/m/Y", strtotime($today. "+1 day")); // Output: 25/08/2016

Please use below code请使用以下代码

<?php
  $today = '24/08/2016';
  $today = explode('/',$today);
  $nextday = date('d/m/Y',mktime(0,0,0,$today[1],$today[0]+1,$today[2])));
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM