繁体   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