php mysql 日期保存,PHP MYSQL - >显示从MYSQL数据库保存为“日期”类型的日期

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 . "
";

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值