1

我正在尝试 t0 在 DATETIME 上执行子字符串,DATETIME 的值是通过 mysql 从数据库中检索的。

例子:

日期时间:2013 年 7 月 31 日 12:30:60 年:2013 年 月:07 日:31 小时:12 分钟:30

我下面的代码不起作用。我该怎么做呢?

 <?php
$sql = "SELECT * FROM auctionItem;";
        // Write a statement to open a connection to MySQL server
        $link = mysql_connect("localhost:3306", "root", "gfg");
        // Write a statement to select the required database
        mysql_select_db("KXCLUSIVE", $link);
        // Write a statement to send the SQL statement to the MySQL server for execution and retrieve the resultset
        $resultset = mysql_query($sql);
        // Write a statement to close the connection
        mysql_close($link);

    $dateTime = $row["startTime"];
    $year = substr($dateTime, 0,4);
    $month = substr($dateTime, 5,7);
    $day = substr($dateTime, 8,10);
    $hour = substr($dateTime, 11,13);
    $minute = substr($dateTime, 14,16);
    echo "year " .$year."<br></br>";
    echo "month " .$month."<br></br>";
    echo "day " .$day."<br></br>";
    echo "hour " .$hour."<br></br>";
    echo "minute " .$minute."<br></br>";
    ?>
4

4 回答 4

2

substr想要作为第三个参数的长度,而不是停止的位置。

正确的是:

$year = substr($dateTime, 0,4);
$month = substr($dateTime, 5,2);
$day = substr($dateTime, 8,2);
$hour = substr($dateTime, 11,2);
$minute = substr($dateTime, 14,2);
于 2013-04-20T23:55:41.067 回答
1

为此使用DateTime该类:

$dt = new DateTime('2013-07-31 12:30:60');
echo "year " .$dt->format('Y')."<br></br>";
echo "month " .$dt->format('m')."<br></br>";
echo "day " .$dt->format('d')."<br></br>";
echo "hour " .$dt->format('H')."<br></br>";
echo "minute " .$dt->format('i')."<br></br>";
于 2013-04-20T23:56:15.273 回答
0

您的代码缺少部分:

<?php
   $sql = "SELECT * FROM auctionItem;";
        // Write a statement to open a connection to MySQL server
        $link = mysql_connect("localhost:3306", "root", "gfg");
        // Write a statement to select the required database
        mysql_select_db("KXCLUSIVE", $link);
        // Write a statement to send the SQL statement to the MySQL server for execution and retrieve the resultset
        $resultset = mysql_query($sql);
        // Write a statement to close the connection
        $row = mysql_fetch_assoc($resultset); //this was lost
        mysql_close($link);

    $dateTime = $row["startTime"];
    $year = substr($dateTime, 0,4);
    $month = substr($dateTime, 5,7);
    $day = substr($dateTime, 8,10);
    $hour = substr($dateTime, 11,13);
    $minute = substr($dateTime, 14,16);
    echo "year " .$year."<br></br>";
    echo "month " .$month."<br></br>";
    echo "day " .$day."<br></br>";
    echo "hour " .$hour."<br></br>";
    echo "minute " .$minute."<br></br>";
?>

PS:不要使用 mysql 扩展....改用 mysqli 或 PDO

于 2013-04-20T23:56:50.460 回答
0

使用strtotime()

$time = '2013-07-31 12:30:60';
$timestamp = strtotime($time);

$year = date('Y', $timestamp);
$month = date('m', $timestamp);
$day = date('d', $timestamp);
$hour = date('H', $timestamp);
$minute = date('i', $timestamp);
$second = date('s', $timestamp);

echo $year.' '.$month.' '.$day.' '.$hour.' '.$minute.' '.$second.'<br>';

使用preg_split()

$time = '2013-07-31 12:30:60';
list($year, $month, $day, $hour, $minute, $second) = preg_split('/-|:|\s/', $time);
echo $year.' '.$month.' '.$day.' '.$hour.' '.$minute.' '.$second;
于 2013-04-21T00:03:05.473 回答