格式化日期并使用php添加或减去来自mysql的日期天数

i am using mysql and and php , currently i have date and time in my database but when i get the date using sql , i keep on getting 1970-01-01 no matter what date , but if i try to add still no luck ! can any one guide me

 $startDate = $result['startDate'];
 $date=date('Y-m-d',$startDate);
 echo $date;

it should be something like 01-07-2011 . but it dosn't i have tried strtotime() also . but it dosnt help . can any one help me .

 $date = strtotime(date("Y-m-d", strtotime($startDate)) . " +1 day");
 echo $date;

answer should be 2011-07-02 but its 1970-01-02

thanks in advance

As long as $result['startDate'] is a date type column, and you're on a version of php greater than 5.0.2, you're looking for:

 $startDate = $result['startDate'];
 $date=date('Y-m-d',strtotime($startDate. ' +1 day'));
 echo $date;

If startDate is datetime type, and you want to use date function on php, you need to change your query to something like :

SELECT UNIX_TIMESTAMP(startDate) as startDate .....

MySQL returns its dates as a 'yyyy-mm-dd hh:mm:ss' string by default. You're probably passing this string directly into date() in PHP, which is incorrect. date() expects a timestamp (seconds since Jan 1/1970). Since you're passing in an invalid date, it's going to default to timestamp 0, aka Jan 1970.

You can force MySQL to return a timestamp suitable for PHP usage with SELECT UNIX_TIMESTAMP(yourdatefield). However, remember that MySQL is perfectly capable of doing date math within a query as well.

If $result['startDate'] is a MySQL Date (or DateTime) then the following will work:

$startDate = $result['startDate'];
$date=date('Y-m-d',strtotime('+1 day', $startDate));
echo $date;