1

im doing some stuff with mktime, i need to add the next date with 30 days more but its returning me 1970-01-30 date, what im doing wrong ?

$strtime=strtotime("2013-10-04");
$fecha=date("Y-m-d",$strtime);
echo $fecha."<br />";
$nueva_fecha=mktime(0,0,0,date("n",$fecha),date("j",$fecha)+30,date("Y",$fecha));
echo date("Y-m-d",$nueva_fecha)."<br />";

Result:

2013-10-04

1970-01-30

3
  • does it have to be mktime? Commented Oct 4, 2013 at 20:43
  • Is there any other way ? i just need to sum 30 days and get the new date with it. Commented Oct 4, 2013 at 20:45
  • FYI, but I got this warning 3x on your mktime call: Notice: A non well formed numeric value encountered in date.php on line 6 Commented Oct 4, 2013 at 20:46

5 Answers 5

2

Date is looking for a timestamp as it's 2nd parameter, not a string value representing this. Updated to pass it $strtime instead.

$strtime=strtotime("2013-10-04");
$fecha=date("Y-m-d",$strtime); // <-- Unnecessary unless you want to echo the value.
echo $fecha."<br />";
$nueva_fecha=mktime(0,0,0,date("n",$strtime),date("j",$strtime)+30,date("Y",$strtime));
echo date("Y-m-d",$nueva_fecha)."<br />";

Output:

2013-10-04

2013-11-03

Sign up to request clarification or add additional context in comments.

Comments

2

You can just use the following function to add 30 days to the date you put in:

$date = strtotime("2013-10-04");
$new_date = strtotime("+30 days", $date);

or simply to the current date:

$new_date = strototime("+30 days", time());

Comments

1

If you already have strtotime, why plus on date ? Instead you could've used + (30 days in seconds) OR simply the feature they offer you + 1 day check answer: adding one day to a date

strtotime('2013-10-04 + 30 days');

This will print 2013-11-03: date('Y-m-d', strtotime('2013-10-04 + 30 days'))

Comments

1

you can try this:

echo strtotime("+1 day"), "\n";

echo strtotime("+30 day",strtotime(date('D, d M Y'))), "\n";

this will add 30 days to the current date.

Also strtotime is very usefull you can use it for weekly,monthly and yearly.

Comments

0

You can use this also

<?php

$date      = date("Y/m/d"); // example date in yyyy/mm/dd format
$unix_time = strtotime( $date ); // covert date to unix time

$sec_in_30_days  = 60 * 60 * 24 * 30; // 60 seconds * 60 minutes * 24 hours * 30 days

$new_unix_time   = $unix_time + $sec_in_30_days; // add 30 days to unix time
$date_in_30_days = date( 'Y/m/d', $new_unix_time ); // convert new unix time to date

// Output results:
echo 'original current date: ' . $date . '<br />';
echo '<br />';
echo 'new date: ' . $date_in_30_days . '<br />';

?>

Output will be

original current date: 2013/10/04

new date: 2013/11/03

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.