【问题标题】:How to insert date and time in mysql and view them如何在mysql中插入日期和时间并查看它们
【发布时间】:2016-02-10 00:27:58
【问题描述】:

我正在尝试制作一个双层博客。我想跟踪注册用户发布博客的日期和时间。稍后将在前端发布博客查看我博客中的时间为时间:2:50 pm/am,日期为日期:2016 年 8 月 7 日。 这是插入代码

 public function save_blog($data, $files) {

    $category_id = $data['category_id'];
    $blog_title = $data['blog_title'];
    $blog_short_description = $data['blog_short_description'];
    $publication_status = $data['publication_status'];
    $blog_long_description = $data['blog_long_description'];

    $path=  '../asset/admin/blog_image/';
    $target_image = $path.$files['blog_image']['name'];
    $file_type = pathinfo($target_image, PATHINFO_EXTENSION);
    $file_size = $files['blog_image']['size'];
    $check = getimagesize($files['blog_image']['tmp_name']);
    if ($check) {
        if (file_exists($target_image)) {
            echo 'Sorry File already exists.';
            exit();
        } else {
            if ($file_size > 1000000) {
                echo 'Sorry uour file Size is too large.';
                exit();
            } else {
                if ($file_type != 'jpg' && $file_type != 'png') {
                    echo 'Sorry your file type is not valid.';
                    exit();
                } else {
                    move_uploaded_file($files['blog_image']['tmp_name'], $target_image);
                    $date=date("M  j, Y, g:i a");
                    try {
                        $query = "INSERT INTO tbl_blog(category_id, blog_title, blog_short_description, blog_long_description, blog_image, publication_status, post_time) VALUES(:category_id, :blog_title, :blog_short_description, :blog_long_description, :blog_image, :publication_status, :date)";
                        $stmt = $this->pdo->prepare($query);
                        $stmt->bindParam(':category_id', $category_id, PDO::PARAM_STR);
                        $stmt->bindParam(':blog_title', $blog_title, PDO::PARAM_STR);
                        $stmt->bindParam(':blog_short_description', $blog_short_description, PDO::PARAM_STR);
                        $stmt->bindParam(':blog_long_description', $blog_long_description, PDO::PARAM_STR);
                        $stmt->bindParam(':blog_image', $target_image, PDO::PARAM_STR);
                        $stmt->bindParam(':publication_status', $publication_status, PDO::PARAM_INT);
                        $stmt->bindParam(':date', $date);
                        $stmt->execute();
                        $message = "Save Blog information successfully";
                        return $message;
                    } catch (PDOException $e) {
                        echo $e->getMessage();
                    }
                }
            }
        }
    }
    else {
    echo 'Sorry ! this is not an image !';
    exit();
    }
}

在 mysql 中,我创建列名称:post_time,类型:DATETIME。这是正确的吗?

如何将时间视为我想要的格式时间:下午 2:50 和日期为日期:2016 年 8 月 7 日

您好,这是更新后的代码,可以正常工作。我的问题是如何跳过我的 .csv 文件的第一行,该文件通常包含标题(id、name、email、address)。

<?php
include 'connection.php';

class Import {
    private $pdo;

    public function __construct() {     
        $obj_connection = new Db_connection();
        $this->pdo = $obj_connection->connection();     
    }

    public function import_csv() {

        $extension= end(explode(".", basename($_FILES['file']['name'])));
        if (isset($_FILES['file']) && $_FILES['file']['size'] < 10485760 && $extension=='csv') {
            $file = $_FILES['file']['tmp_name']; 
            $handle = fopen($file, "r"); 
            try {           
                while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {                   
                $importSQL = "INSERT INTO tbl_applicants ( application_no, applicant_name, applicant_email, applicant_mobile, applicant_address ) VALUES('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')";
                $this->pdo->query($importSQL);  
                }
            }
            catch(PDOException $e) {
                echo $e->getMessage();
            }

        }
    }

}

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    如果您在 php 中使用 DATETIME 默认日期时间存储方法,并且您希望将其显示为您想要的格式,例如 2:50 am/pm,那么:

    =>你想显示的地方首先使用php的默认函数将这个时间转换为时间戳,即strtotime($time)

    一旦将时间转换为时间戳,您就可以使用 php 默认日期函数 date($format, $timestamp) 轻松将此时间戳格式化为您想要的格式。

    欲了解更多信息,请访问以下链接:

    PHP DATE FUNCTION

    【讨论】:

    • 你说的是这个吗?此代码显示 1970 年 1 月 1 日。 $time=strtotime($blog_info['post_time']); echo date("M j, Y", $time);
    • 您能告诉我您是如何存储时间的吗?是指存储在数据库中的值吗?
    • $date=date("M j, Y, g:i a"); $query = "INSERT INTO tbl_blog(post_time) VALUES($date)";在数据库中我创建一个列名:post_time,输入:DATETIME,定义:当前时间戳
    • 如果您使用 mysql DATETIME 字段类型,那么您必须以特定格式存储您的自定义日期,否则它将返回 1970 年 1 月 1 日。要解决这种情况,请将字段类型更改为 VARCHAR .
    • 谢谢,现在可以了。它向我显示 UTC 时间,我怎样才能使这个 GMT +6?
    【解决方案2】:

    为此目的使用TIME_FORMAT(time,format)DATE_FORMAT(date,format) 函数。一个例子是

    SELECT DATE_FORMAT(post_time, '%b %e %Y %r') AS Newdate
    FROM tbl_blog;
    

    【讨论】:

      【解决方案3】:

      我认为有一个简单的方法可以做到这一点。您可以使用 SQL 函数“CURDATE()”获取当前日期,使用 CURTIME() 获取当前时间。然后只需 INSERT 到您的表中。 像这样。

      $q ="选择 CURDATE()"; $curdate = mysql_query($q); 然后 mysql_query("INSERT INTO table(column_name) VALUES $curdate"); 对时间做同样的事情。 这是一个非常基本的方法。希望你能找到帮助。

      【讨论】:

        猜你喜欢
        • 2018-06-05
        • 2011-02-25
        • 2018-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多