2014-01-05 15 views
1

我在新聞部分遇到問題。時間的數量是正確的,但是當它實際上是下午時間,它發佈了上午,反之亦然。在php上發佈反向時間

這裏的代碼:

insertnews.php

<?php 

$news = $_POST['news']; 

date_default_timezone_set('Asia/Manila'); 
$dateposted = date('Y/m/d h:i:s a', time()); 

$con = mysqli_connect("localhost", "root", "root", "chess"); 

if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 


mysqli_query($con, "INSERT into news(dateposted,news) values('$dateposted', '$news')"); 

header('location: newslist.php'); 

?> 

newslist.php

<?php 
mysql_connect("localhost", "root", "root") or die(mysql_error()); 
mysql_select_db("chess") or die(mysql_error()); 

$result = mysql_query("SELECT * FROM news") 
or die(mysql_error()); 

$numrows = mysql_num_rows($result); 

if ($numrows == 0) { 
    echo "No news"; 
} else { 
    echo "<table border=1>"; 
    echo "<tr><td><b>Date Posted</b></td><td><b>News</b></td></tr>"; 

    while ($row = mysql_fetch_array($result)) { 
     date_default_timezone_set('Asia/Manila'); 
     $currentDateTime = $row['dateposted']; 
     $newDateTime  = date('Y/m/d h:i:s a', strtotime($currentDateTime)); 
     echo "<tr>" . "<td>" . $newDateTime . "</td> <td>" . $row['news'] . "</td><td><a href='editnews.php?id=" . $row['id'] . "'>Edit</a></td><td><a href='deletenews.php?id=" . $row['id'] . "'>Delete</a></td></tr>"; 


    } 
    echo "</table>"; 
} 

mysql_close(); 
?> 

,如果我如何解決這個問題,我不明白。

+0

'的var_dump($ currentDateTime)刪除此行

date_default_timezone_set('Asia/Manila'); 

;' - 您能得到什麼? –

+0

請切換到[已準備好的語句](http://bobby-tables.com/php.html)以防止[SQL注入](https://www.owasp.org/index.php/SQL_Injection)。將時間節省作爲時間戳不是更好嗎?這樣,您可以輕鬆調整日期顯示日期。 –

+0

@AmalMurali'string'2014-01-06 12:02:16'(length = 19) string'2014-01-06 12:01:44'(length = 19) string'2014-01-06 12 :12:21'(length = 19) string'2014-01-06 12:20:54'(length = 19) string'2014-01-06 12:32:53'(length = 19)' – pingboo23

回答

0

試試這個

$date = new DateTime('now'); 
$date->setTimezone(new DateTimeZone('Asia/Manila')); 
echo $date->format('Y/m/d h:i:s a'); 

從這裏

while ($row = mysql_fetch_array($result)) { 
     date_default_timezone_set('Asia/Manila'); //don't need this line anymore 
     $currentDateTime = $row['dateposted']; 
     $newDateTime  = date('Y/m/d h:i:s a', strtotime($currentDateTime)); 
     echo "<tr>" . "<td>" . $newDateTime . "</td> <td>" . $row['news'] . "</td><td><a href='editnews.php?id=" . $row['id'] . "'>Edit</a></td><td><a href='deletenews.php?id=" . $row['id'] . "'>Delete</a></td></tr>"; 
} 
+0

嗨。我試過你的代碼,但仍然是一樣的。 – pingboo23