【问题标题】:Date in the data base comes in 10 digits数据库中的日期为 10 位数字
【发布时间】:2014-09-24 12:59:52
【问题描述】:

我是 PHP / MySQL 的新手。

我在网上做了这个教程:

php mysql 中的会员区系统-会员用户系统区登录签到

http://www.webestools.com/scripts_tutorials-code-source-14-members-area-system-in-php-mysql-members-users-system-area-log-sign.html

到目前为止一切正常,除了日期。

日期(signup_date 列)在数据库中以一种奇怪的方式出现。我该如何解决?

id: 1 用户名:我的用户名 密码:helloworld 电子邮件:我的用户名 头像:我的头像 注册日期:1411562966

非常感谢您的帮助

这是我的mysql表代码:

CREATE TABLE IF NOT EXISTS `users` (

id bigint(20) 非空, username varchar(255) 非空, password varchar(255) 非空, email varchar(255) 非空, avatar 文本不为空, signup_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=MyISAM 默认字符集=utf8;

这是我想我必须修改日期的 sign_up.php 页面:

    <?php
include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link href="<?php echo $design; ?>/style.css" rel="stylesheet" title="Style" />
        <title>Sign up</title>
    </head>
    <body>
        <div class="header">
                <a href="<?php echo $url_home; ?>"><img src="<?php echo $design; ?>/images/logo.png" alt="Members Area" /></a>
            </div>
<?php
//We check if the form has been sent
if(isset($_POST['username'], $_POST['password'], $_POST['passverif'], $_POST['email'], $_POST['avatar']) and $_POST['username']!='')
{
        //We remove slashes depending on the configuration
        if(get_magic_quotes_gpc())
        {
                $_POST['username'] = stripslashes($_POST['username']);
                $_POST['password'] = stripslashes($_POST['password']);
                $_POST['passverif'] = stripslashes($_POST['passverif']);
                $_POST['email'] = stripslashes($_POST['email']);
                $_POST['avatar'] = stripslashes($_POST['avatar']);
        }
        //We check if the two passwords are identical
        if($_POST['password']==$_POST['passverif'])
        {
                //We check if the password has 6 or more characters
                if(strlen($_POST['password'])>=6)
                {
                        //We check if the email form is valid
                        if(preg_match('#^(([a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+\.?)*[a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+)@(([a-z0-9-_]+\.?)*[a-z0-9-_]+)\.[a-z]{2,}$#i',$_POST['email']))
                        {
                                //We protect the variables
                                $username = mysql_real_escape_string($_POST['username']);
                                $password = mysql_real_escape_string($_POST['password']);
                                $email = mysql_real_escape_string($_POST['email']);
                                $avatar = mysql_real_escape_string($_POST['avatar']);
                                //We check if there is no other user using the same username
                                $dn = mysql_num_rows(mysql_query('select id from users where username="'.$username.'"'));
                                if($dn==0)
                                {
                                        //We count the number of users to give an ID to this one
                                        $dn2 = mysql_num_rows(mysql_query('select id from users'));
                                        $id = $dn2+1;
                                        //We save the informations to the databse
                                        if(mysql_query('insert into users(id, username, password, email, avatar, signup_date) values ('.$id.', "'.$username.'", "'.$password.'", "'.$email.'", "'.$avatar.'", "'.time().'")'))
                                        {
                                                //We dont display the form
                                                $form = false;
?>
<div class="message">You have successfuly been signed up. You can log in.<br />
<a href="connexion.php">Log in</a></div>
<?php
                                        }
                                        else
                                        {
                                                //Otherwise, we say that an error occured
                                                $form = true;
                                                $message = 'An error occurred while signing up.';
                                        }
                                }
                                else
                                {
                                        //Otherwise, we say the username is not available
                                        $form = true;
                                        $message = 'The username you want to use is not available, please choose another one.';
                                }
                        }
                        else
                        {
                                //Otherwise, we say the email is not valid
                                $form = true;
                                $message = 'The email you entered is not valid.';
                        }
                }
                else
                {
                        //Otherwise, we say the password is too short
                        $form = true;
                        $message = 'Your password must contain at least 6 characters.';
                }
        }
        else
        {
                //Otherwise, we say the passwords are not identical
                $form = true;
                $message = 'The passwords you entered are not identical.';
        }
}
else
{
        $form = true;
}
if($form)
{
        //We display a message if necessary
        if(isset($message))
        {
                echo '<div class="message">'.$message.'</div>';
        }
        //We display the form
?>
<div class="content">
    <form action="sign_up.php" method="post">
        Please fill the following form to sign up:<br />
        <div class="center">
            <label for="username">Username</label><input type="text" name="username" value="<?php if(isset($_POST['username'])){echo htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8');} ?>" /><br />
            <label for="password">Password<span class="small">(6 characters min.)</span></label><input type="password" name="password" /><br />
            <label for="passverif">Password<span class="small">(verification)</span></label><input type="password" name="passverif" /><br />
            <label for="email">Email</label><input type="text" name="email" value="<?php if(isset($_POST['email'])){echo htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8');} ?>" /><br />
            <label for="avatar">Avatar<span class="small">(optional)</span></label><input type="text" name="avatar" value="<?php if(isset($_POST['avatar'])){echo htmlentities($_POST['avatar'], ENT_QUOTES, 'UTF-8');} ?>" /><br />
            <input type="submit" value="Sign up" />
                </div>
    </form>
</div>
<?php
}
?>
                <div class="foot"><a href="<?php echo $url_home; ?>">Go Home</a> - <a href="http://www.webestools.com/">Webestools</a></div>
        </body>
</html>

【问题讨论】:

  • 您应该将signup_date 数据类型更改为datetime 并在插入查询中使用date("Y-m-d H:i:s") 或mysql 函数now() 而不是time()。这将在您以后需要执行的查询方面做得更好,而不必来回更改时间戳。
  • 谢谢。它有效 ;-) 非常感谢。

标签: php mysql date format digits


【解决方案1】:

这是一个 Unix 风格的时间戳(又名 Epoch time),表示日期为 2014 年 9 月 24 日星期三 12:49:26 GMT。它是通过计算自 UTC 时间 1970 年 1 月 1 日午夜以来的秒数来计算的。

您可以通过几种不同的方式在 MySQL 中represent dates,而不仅仅是存储一个 int。

【讨论】:

    【解决方案2】:

    将注册日期更改为

    signup_datetimestamp NOT NULL

    【讨论】:

    • 我刚做了,现在日期是这样的:signup_date: 0000-00-00 00:00:00
    • 保存另一个数据。日期将采用以下格式:年-月-日时:分:秒 yyyy-mm-dd h:m:s
    • 如果不起作用,请将用于保存表单值的 php 代码粘贴到此处。
    • 当您将signup_date更改为时间戳时,表中保存的数据将更改为0000-00-00 00:00:00,但如果您保存另一个表单数据,您将有yyyy-mm-dd时:分:秒
    猜你喜欢
    • 2013-03-13
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多