Here is my code:
$graceperiod = strtotime("+30 day","23-12-2013");
echo $graceperiod;
I get the following output:
Sat, 31 Jan 1970 00:00:23 GMT
The year is wrong. Any idea why it is not converting properly?
Here is my code:
$graceperiod = strtotime("+30 day","23-12-2013");
echo $graceperiod;
I get the following output:
Sat, 31 Jan 1970 00:00:23 GMT
The year is wrong. Any idea why it is not converting properly?
strtotime is a "dangerous" function if you don't know what it does exactly. You should try it like this
strtotime('+30 days', $timestamp);
Where $timestamp is an actual timestamp, because it's not as reliable as you may wish
strtotime
takes the Unix timestamp as its second parameter, so you need to convert it first.
The following has the correct output:
$graceperiod = date("Y-m-d", strtotime("+30 days",strtotime("23-12-2013")));
echo $graceperiod;
You can either use the good old strtotime()
function (which accepts a timestamp as second parameter) or use the DateTime
classes:
Using strtotime()
$format = 'd-m-Y';
$timestamp = strtotime( "+30 day",strtotime( "23-12-2013" ) );
echo date( $format, $timestamp );
Using DateTime
classes:
$dateTime = DateTime::createFromFormat( $format, '23-12-2013' );
$dateTime->add( new DateInterval( 'P30D' ) );
echo $dateTime->format( $format );
Here the P30D
means a period of 30 days
One advantage of using DateTime
is you can define your own format instead of using from the list of accepted formats for strtotime()
strtotime
takes second parammeter as timestamp, so you should do:
$graceperiod = strtotime("+30 day", strtotime("23-12-2013"));
echo date("Y-m-d H:i:s", $graceperiod);
Try the following code.
$date = "23-12-2013";
$graceperiod = date("Y-m-d", strtotime("+30 days",strtotime($date)));
echo $graceperiod;
Code:
$graceperiod = strtotime('23-12-2013 +30 day');
var_dump(date('Y-m-d', $graceperiod));
Result:
string(10) "2014-01-22"