PHP MYSQL - >显示从MYSQL数据库保存为“日期”类型的日期 [英] PHP MYSQL -> Display dates saved as 'date' type from MYSQL Database
问题描述
我有一个名为employee_datetable的数据库表,其中一个字段称为 datetime包含下一年365天(2014年8月13日至2015年8月13日)的实际日期记录。
我想编写一个查询7个日期的MYSQL查询,并以可读格式(如2014年9月24日)连续显示。
现在,日期存储为日期类型,并显示为2014-09-24等。
任何帮助将不胜感激。
PHP代码:
$ dates_sql =SELECT UNIX_TIMESTAMP(datetime)FROM employee_datetable LIMIT 7;
$ PhpDate = strtotime($ dates_sql);
$ FormattedPhpDate = date('M d,Y',$ PhpDate);
echo查询中选择的日期:。 $ FormattedPhpDate;
您需要使用类似 mysqli 或 pdo 执行查询并提取数据。这是一般的想法,使用 mysqli
:
$ mysqli = new mysqli (localhost,my_user,my_password,world);
$ dates_sql =SELECT UNIX_TIMESTAMP(datetime)AS tstamp FROM employee_datetable LIMIT 7;
$ result = $ mysqli-> query($ dates_sql);
while($ row = $ result-> fetch_array()){
$ FormattedPhpDate = date('M d,Y',$ row ['tstamp']);
echoDate:。 $ FormattedPhpDate。 < br />;
}
注意:由于您使用 UNIX_TIMESTAMP
在您的查询中,不需要使用 strtotime()
将日期转换为时间戳
有关详细信息,请参阅 mysqli ,特别是 mysqli_query 和 mysqli_fetch_array
I'm still getting used to using PHP and MYSQL so please bear with me.
I have a database table called 'employee_datetable' that with a field called "datetime" contains 365 records of actual dates for the next year (August 13, 2014 to August 13, 2015).
I want to write a MYSQL query that retrieves 7 dates and displays them in a row while in readable format such as "Sept 24, 2014".
Right now the dates are stored as date types and displayed as "2014-09-24" etc.
Any help would be greatly appreciated.
PHP Code:
$dates_sql = "SELECT UNIX_TIMESTAMP(datetime) FROM employee_datetable LIMIT 7";
$PhpDate = strtotime($dates_sql);
$FormattedPhpDate = date('M d, Y', $PhpDate );
echo "Selected Dates from the query: " . $FormattedPhpDate;
You need to use something like mysqli or pdo to execute your query and extract the data. Here's the general idea, using mysqli
:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$dates_sql = "SELECT UNIX_TIMESTAMP(datetime) AS tstamp FROM employee_datetable LIMIT 7";
$result = $mysqli->query($dates_sql);
while($row = $result->fetch_array()) {
$FormattedPhpDate = date('M d, Y', $row['tstamp']);
echo "Date: " . $FormattedPhpDate . "<br/>";
}
Note: Since you are using UNIX_TIMESTAMP
in your query there is no need to convert the date to a timestamp using strtotime()
For more info see documentation for mysqli, specifically mysqli_query and mysqli_fetch_array
这篇关于PHP MYSQL - >显示从MYSQL数据库保存为“日期”类型的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!