2013-04-20 76 views
1

我想t0在DATETIME上執行一個子串,DATETIME的值是通過mysql從數據庫中檢索的。php中的子串通過值從數據庫中檢索

實施例:

DATETIME: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>"; 
    ?> 
+0

什麼字段類型是'$行[ 「開始時間」]'存儲? MySQL也有日期函數。 – nickhar 2013-04-21 00:00:15

+0

如果您使用PHP> = 5.2 ..最好的方式來做到這一點是通過使用日期時間::格式..檢查了這一點http://us3.php.net/datetime.format – Dinesh 2013-04-21 00:00:27

+0

@Dinesh日期時間存在爲PHP 5.3 not 5.2 – bwoebi 2013-04-21 00:01:01

回答

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

即將響應此.. substr中的第二個值是一個長度,而不是一個位置 – Silvertiger 2013-04-20 23:56:56

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

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;