簡體   English   中英

以dmY格式顯示MySQL數據庫中的日期

[英]Displaying date from MySQL database in the d-m-Y format

我正在使用PHP和MySQL為新聞網站進行編碼。 我想以dmY格式顯示日期。 為此,我使用了:

$date='<td>'.$row['date'].'</td>'; //'date' is the value from the database
$newdate=strtotime($date);      

$showdate=date("d-m-Y",$newdate);

       echo $showdate;

但是,對於所有提取,我只能將日期定為1-1-1970。 問題是什么?

這不僅可以避免黑客入侵。 您還需要檢查來自后端(PHP)的用戶輸入。

這是關於驗證用戶輸入的文章: https : //www.dreamhost.com/blog/2013/05/22/php-security-user-validation-and-sanitization-for-the-beginner/

您還可以閱讀有關sql注入的信息: http : //php.net/manual/zh/security.database.sql-injection.php

關於表單操作屬性: http : //www.sitepoint.com/web-foundations/action-html-attribute/

現在關於表單方法:

定義和用法

formmethod屬性定義用於將表單數據發送到操作URL的HTTP方法。

formmethod屬性覆蓋元素的method屬性。

注意:formmethod屬性可以與type =“ submit”和type =“ image”一起使用。

表單數據可以作為URL變量(method =“ get”)或作為HTTP post事務(method =“ post”)發送。

關於“獲取”方法的注意事項:

 This method appends the form-data to the URL in name/value pairs This method is useful for form submissions where a user want to bookmark the result There is a limit to how much data you can place in a URL (varies between browsers), therefore, you cannot be sure that all of the 

表單數據將正確傳輸切勿使用“獲取”方法傳遞敏感信息! (密碼或其他敏感信息將在瀏覽器的地址欄中顯示)

關於“ post”方法的注釋:

 This method sends the form-data as an HTTP post transaction Form submissions with the "post" method cannot be bookmarked The "post" method is more robust and secure than "get", and "post" does not have size limitations 

資料來源: http : //www.w3schools.com/tags/att_input_formmethod.asp

都不行 要將數據從一種形式傳遞到php腳本,只需創建過濾字段的響應腳本,就可以使用strip_tagsfilter_var

HTML

  <form action="myscript.php" method="post">
       <!-- your values -->
    </form>

您的PHP腳本

if(isset($_POST['your_input_name'])):
    $name = strip_tags($_POST['your_input_name']);
    $name = filter_var($name, FILTER_SANITIZE_STRING);
    # Do something with your input..
endif;

您的代碼使用幾個不同的字段作為日期,您並沒有真正說出它來自數據庫的日期。 但是,我想我可以用一個適用於您的案例的通用示例來回答這個問題。 對於此答案,我們假設您有一個名為$ date的變量,該變量保存數據庫中的值。

echo date('d-m-Y', strtotime($date));

正確的代碼:

$ dbdate = $ row ['date']; [這將保存MySql數據庫中的日期。]

$ newdate = strtotime($ dbdate);

$ showdate = date(“ dmY”,$ newdate);

   echo $showdate;

我有正確的結果。

請添加如下內容。.這里首先檢查您從數據庫中獲得的$ row ['date']中的值。.如果您在此處獲得日期,請使用以下幾行轉換dmY格式

$date=date_create($row['date']); // but here first check value of $row['date']
echo date_format($date,"d-m-Y");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM