raar07
raar07

Reputation: 35

how to get the next year date from the date value stored in a php variable

I am trying to get the next year date from a php variable which holds the date value posted from a html form. Below is the code

    $warranty_from = mysql_real_escape_string($_POST['from_date']);

Say the $warranty_from holds the value 2013-06-04. I want to get the next year date i.e 2014-06-04 and store into $warranty_to variable. Searched lot over the net but couldnt find any resource related to my issue. Any help is appreciated. Thanks in advance.

Upvotes: 3

Views: 153

Answers (4)

vascowhite
vascowhite

Reputation: 18440

Calculations like this are trivial using the DateTime classes:-

$_POST['from_date'] = '2013-06-04';//Just for this example.
$warranty = DateTime::createFromFormat('Y-m-d', $_POST['from_date']);
$interval = new DateInterval('P1Y');
$warranty->add($interval);

echo $warranty->format('Y-m-d');

Will output:-

2014-06-04

Upvotes: 0

Neta Meta
Neta Meta

Reputation: 4047

The following should work:

$warranty_from = '2013-06-04';
$warranty_to = date('y-m-d', strtotime('+1 years', strtotime($warranty_from)));

Something like that should work

Upvotes: 5

Mahesh
Mahesh

Reputation: 320

Try this one

<?php
 $warranty_to = date('Y-m-d', strtotime('+1 year', strtotime($warranty_from)));
?>

Upvotes: 0

ghmulchandani
ghmulchandani

Reputation: 304

This was exact code

$newWarranty_date = date("Y-m-d",strtotime("+1 year",strtotime($warranty_from)));

Upvotes: 0

Related Questions