繁体   English   中英

通过从数据库中检索值的PHP中的子串

[英]Substring in php through value retrieve from database

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

例:

日期:2013-07-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>";
    ?>

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);

使用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>";

您的代码缺少部分:

<?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

使用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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM