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
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.
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;
如果startDate
是datetime
类型,并且要在php上使用date
函数,则需要将查询更改为:
SELECT UNIX_TIMESTAMP(startDate) as startDate .....
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.