doushi3322 2014-04-24 02:31
浏览 47
已采纳

从sql数据库获取月,日,年的php时间

1398305717 <- my sql database time code like this

mysql time row name is time

.php

$numMonth = date('m', $time);

$sql = mysql_query("SELECT * FROM videos WHERE date('m',`time`) = $numMonth DESC LIMIT 1");
$row = mysql_fetch_assoc($sql);
echo $id = $row['id'];

i want to get last id equals to month numer? so i tried to like this.. but not work. how to get it from php. so help me to this post how to get month number from sql.. thanks.

Edit : i want like this.. but not work :(

$sql = mysql_query("SELECT * FROM videos WHERE MONTH(FROM_UNIXTIME(`time`)) = '$numMonth' DESC LIMIT 1");
  • 写回答

1条回答 默认 最新

  • drymoeuka282427675 2014-04-24 02:35
    关注

    you have to convert the timestamp then extract the month. and you don't need to use php date functions to get the current month you can just use mysql's functions to do it

    $sql = mysql_query("SELECT *, DATE_FORMAT(FROM_UNIXTIME(`time`),'%Y') as year,DATE_FORMAT(FROM_UNIXTIME(`time`),'%m') as month FROM videos WHERE MONTH(FROM_UNIXTIME(`time`)) = MONTH(CURDATE()) ORDER BY ID  LIMIT 1");
    

    working demo: http://sqlfiddle.com/#!2/b4e465/8

    don't forget about year, if you expect multiple years of timestamps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序