仅从datetime字段(mysql)中提取日期并将其分配给php变量

时间:2021-01-26 15:43:42

I am unable to assign only date to a php variable from datetime field of a MySQL table. I am using PHP 4, I know how to do it in PHP 5.

我无法仅从MySQL表的datetime字段为php变量分配日期。我使用PHP 4,我知道如何在PHP 5中完成它。

Can someone help as to what is missing from the below php code.

有人可以帮助从下面的PHP代码中缺少什么。

$dc = mysql_query("SELECT * FROM INVHDR WHERE Invdate BETWEEN '2011-04-01' AND '2012-03-31'");
while ($dc2 = mysql_fetch_assoc($dc)) {
    $invno      = $dc2['Invno']; // Here Invno is a datetime field type
    $invdat3    = $dc2['Invdate'];
    $date_array = explode("-", $invdat3);
    $invdat     = sprintf('%02d/%02d/%4d', $date_array[2], $date_array[1], $date_array[0]);
}

3 个解决方案

#1


29  

If you just want to display the date portion, you could use the date function:

如果您只想显示日期部分,可以使用日期功能:

echo date("d/m/Y", strtotime("2012-12-24 12:13:14"));

#2


5  

There is a link I find extremely useful when it comes to date manipulations: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

在日期操作方面,我发现有一个非常有用的链接:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

one can easily find the solution there:

人们可以很容易地找到解决方案:

SELECT *, DATE(Invdate) as Inv_date ...

#3


3  

Do it directly in MySQL:

直接在MySQL中执行:

select DATE_FORMAT('2012-12-24 12:13:14', '%Y/%m/%d')

So your query will look like this:

所以你的查询将如下所示:

SELECT DATE_FORMAT(Invdate, '%Y/%m/%d') FROM INVHDR WHERE Invdate BETWEEN '$startdat' AND '$enddat'

#1


29  

If you just want to display the date portion, you could use the date function:

如果您只想显示日期部分,可以使用日期功能:

echo date("d/m/Y", strtotime("2012-12-24 12:13:14"));

#2


5  

There is a link I find extremely useful when it comes to date manipulations: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

在日期操作方面,我发现有一个非常有用的链接:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

one can easily find the solution there:

人们可以很容易地找到解决方案:

SELECT *, DATE(Invdate) as Inv_date ...

#3


3  

Do it directly in MySQL:

直接在MySQL中执行:

select DATE_FORMAT('2012-12-24 12:13:14', '%Y/%m/%d')

So your query will look like this:

所以你的查询将如下所示:

SELECT DATE_FORMAT(Invdate, '%Y/%m/%d') FROM INVHDR WHERE Invdate BETWEEN '$startdat' AND '$enddat'