简体   繁体   English

PHP - 添加 1 天到日期格式 mm-dd-yyyy

[英]PHP - add 1 day to date format mm-dd-yyyy

<?php
    $date = "04-15-2013";
    $date = strtotime($date);
    $date = strtotime("+1 day", $date);
    echo date('m-d-Y', $date);
?>

This is driving me crazy and seems so simple.这让我发疯,看起来很简单。 I'm pretty new to PHP, but I can't figure this out.我对 PHP 很陌生,但我想不通。 The echo returns 01-01-1970 .回声返回01-01-1970

The $date will be coming from a POST in the format mdY , I need to add one day and have it as a new variable to be used later. $date 将来自格式mdYPOST ,我需要添加一天并将其作为一个新变量以备后用。

Do I have to convert $date to Ymd , add 1 day, then convert back to mdY ?我是否必须将 $date 转换为Ymd ,加上 1 天,然后再转换回mdY Would I be better off learning how to use DateTime ?学习如何使用DateTime会更好吗?

there you go 你去吧

$date = "04-15-2013";
$date1 = str_replace('-', '/', $date);
$tomorrow = date('m-d-Y',strtotime($date1 . "+1 days"));

echo $tomorrow;

this will output 这将输出

04-16-2013

Documentation for both function 两种功能的文档
date 日期
strtotime 的strtotime

$date = DateTime::createFromFormat('m-d-Y', '04-15-2013');
$date->modify('+1 day');
echo $date->format('m-d-Y');

See it in action 看到它在行动

Or in PHP 5.4+ 或者在PHP 5.4+中

echo (DateTime::createFromFormat('m-d-Y', '04-15-2013'))->modify('+1 day')->format('m-d-Y');

reference 参考

$date = strtotime("+1 day");
echo date('m-d-y',$date);

use http://www.php.net/manual/en/datetime.add.php like 使用http://www.php.net/manual/en/datetime.add.php之类的

$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('1 days'));
echo date_format($date, 'Y-m-d');

output 产量

2000-01-2

The format you've used is not recognized by strtotime(). strtotime()无法识别您使用的格式。 Replace 更换

$date = "04-15-2013";

by 通过

$date = "04/15/2013";

Or if you want to use - then use the following line with the year in front: 或者如果你想使用-那么使用前面年份的以下行:

$date = "2013-04-15";

Actually I wanted same alike thing, To get one year backward date, for a given date! 实际上我想要相同的东西,为了给定日期一年的落后日期! :-) :-)

With the hint of above answer from @mohammad mohsenipur I got to the following link , via his given link! 从@mohammad mohsenipur我有以下上面的回答的提示链接 ,通过他给出的链接!

Luckily, there is a method same as date_add method, named date_sub method! 幸运的是,有一个方法与date_add方法相同,名为date_sub方法! :-) I do the following to get done what I wanted! :-)我做以下事情来完成我想要的!

$date = date_create('2000-01-01');
date_sub($date, date_interval_create_from_date_string('1 years'));
echo date_format($date, 'Y-m-d');

Hopes this answer will help somebody too! 希望这个答案也能帮到别人! :-) :-)

Good luck guys! 祝大家好运!

Try with below code:尝试使用以下代码:

<?php
   $date = "2022-01-05";
   $to_date = date('Y-m-d', strtotime($date.' +1 day')); 
   echo $to_date;
?>

Output:

2022-01-06

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

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