[英]format date and add or subtract number of days in date coming from mysql using php
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 ! 我正在使用mysql和php,目前我的数据库中有日期和时间,但是当我使用sql获取日期时,无论什么日期,我都会继续获取1970-01-01,但是如果我尝试添加运气的话! can any one guide me
谁能指导我
$startDate = $result['startDate'];
$date=date('Y-m-d',$startDate);
echo $date;
it should be something like 01-07-2011 . 它应该类似于01-07-2011。 but it dosn't i have tried strtotime() also .
但是我也没有尝试过strtotime()。 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 答案应该是2011-07-02,但应该是1970-01-02
thanks in advance 提前致谢
MySQL returns its dates as a 'yyyy-mm-dd hh:mm:ss' string by default. MySQL默认以“ yyyy-mm-dd hh:mm:ss”字符串返回其日期。 You're probably passing this string directly into
date()
in PHP, which is incorrect. 您可能将此字符串直接传递到PHP中的
date()
中,这是不正确的。 date() expects a timestamp (seconds since Jan 1/1970). date()需要时间戳(自1970年1月1日以来的秒数)。 Since you're passing in an invalid date, it's going to default to timestamp 0, aka Jan 1970.
由于您传递的日期无效,因此默认为时间戳0,也就是1970年1月。
You can force MySQL to return a timestamp suitable for PHP usage with SELECT UNIX_TIMESTAMP(yourdatefield)
. 您可以使用
SELECT UNIX_TIMESTAMP(yourdatefield)
强制MySQL返回适合PHP使用的时间戳。 However, remember that MySQL is perfectly capable of doing date math within a query as well. 但是,请记住,MySQL完全能够在查询中执行日期数学运算。
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: 只要$ result ['startDate']是日期类型列,并且您使用的PHP版本大于5.0.2,就在寻找:
$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: 如果
$result['startDate']
是MySQL日期(或DateTime),则可以使用以下命令:
$startDate = $result['startDate'];
$date=date('Y-m-d',strtotime('+1 day', $startDate));
echo $date;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.