日期错误解决方案

This is my program :

$newEndingDate = strtotime(date("Y-m-d", strtotime($row['startdate']))."+1 year");
echo $newEndingDate;

this is my error

1332441000

whats the error

The strtotime() function returns an UNIX Timestamp : the number of seconds since 1970-01-01.

The number you are getting is not an error : it just means that 1332441000 seconds have passed since 1970.


If necessary, you can use the date() function to format that timestamp, to get a string that looks a bit more user-friendly.

For instance :

echo date('Y-m-d H:i:s', $newEndingDate);

should get you the following result :

2012-03-22 19:30:00

It is the return value of strtotime function, which is a UNIX timestamp. The seconds passed since 01/01/1970

I think this is you would like to get:

$newEndingDate = date('Y-m-d', strtotime('+ 1 year', strtotime($row['startdate'])));

Try using mktime to do what you want. Assume $row['startdate'] == '2011-01-01'

$mydate = strtotime($row['startdate']);
$newEndingDate = mktime(date("H", $mydate), date("i", $mydate), date("s", $mydate), date("n", $mydate), date("j", $mydate), date("Y", $mydate) + 1);
echo date('Y-m-d', $newEndingDate); //2012-01-01

What you are doing will not give you the result you're after because you can't do the following:

strotime('2011-01-01+1 year');

Try this instead:

$mydate = strtotime($row['startdate']);
$newEndingDate = date('Y-m-d', strtotime("+1 year", $mydate));
echo $newEndingDate; //2012-01-01

you dont get an error, you get the timestamp of all the seconds that have passed since 1970/01/01

if you need a readable format try:

$date = date("Y-m-d H:i:s", $newEndingDate);