Pregunta

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

¿Fue útil?

Solución

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

Otros consejos

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());

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'))

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.

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top