简体   繁体   English

如何在PHP中的选定日期值中添加1年

[英]how to add 1 year in selected date value in php

I am trying to add 1 year in selected date but I cannot get the output. 我正在尝试在所选日期增加1年,但无法获得输出。

This is what I tried so far: 这是我到目前为止尝试过的:

I am getting date from datepicker in textbox and store it in variable like the following: 我从文本框中的日期选择器获取日期,并将其存储在变量中,如下所示:

$sdate = mysqli_real_escape_string($conn,$_POST['amc_date']);

I tried this code but it doesn't work. 我试过了这段代码,但是没有用。

$edate = date('d/m/Y', strtotime('+1 year', strtotime($sdate)));

Please help. 请帮忙。 Thanks. 谢谢。

$futureDate=date('Y-m-d',strtotime('+1 year'));

$futureDate is one year from now!

$futureDate=date('Y-m-d',strtotime('+1 year',strtotime($startDate)));

$futureDate is one year from $startDate!

You can use DateTime object for this. 您可以为此使用DateTime对象。

// $sdate = '2018-04-02'
$sdate = mysqli_real_escape_string($conn,$_POST['amc_date']);
$dateTime = new \DateTime($sdate);
$resultDate = $dateTime->modify('+1 year')->format('d/m/Y');
// $resultDate = '02/04/2019'

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

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