Nishan Singha
Nishan Singha

Reputation: 169

How to insert date and time in mysql and view them

I am trying to make a double layer blog.I want to keep track of date and time when a registered user post a blog. Later when blog will be published in the front-end View the time in my blog as Time: 2:50 pm/am and date as Date: Aug 7 2016. Here is Insert code

 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();
    }
}

In mysql i make column Name: post_time, Type: DATETIME. Is this correct?

How can view time as my desired format Time: 2:50 pm/am and date as Date: Aug 7 2016

Hi, here is the updated code which works fine. My question is how can i skip the first row of my .csv file which generally contains header( 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();
            }

        }
    }

}

Upvotes: 3

Views: 325

Answers (3)

user5310490
user5310490

Reputation:

There is an easy way to do it I think. You can use SQL function "CURDATE()" to get the cussrent date and CURTIME() to get the current time. Then simply INSERT into your table. like this.

$q ="select CURDATE()"; $curdate = mysql_query($q); then mysql_query("INSERT INTO table(column_name) VALUES $curdate"); Do the same thing for Time. This a very basic approach. Hope you find help.

Upvotes: 2

Rahul
Rahul

Reputation: 77866

Use TIME_FORMAT(time,format) and DATE_FORMAT(date,format) function for this purpose. An example would be

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

Upvotes: 2

Rajesh Gautam
Rajesh Gautam

Reputation: 48

If you are using DATETIME default date time storing method in php and you want to display this your desired format like 2:50 am/pm, then:

=> Where you want to display convert this time to timestamp first by using default function of php i.e. strtotime($time)

once time is converted into timestamp you can easily format this timestamp into your desired format by using php default date function date($format, $timestamp).

For more information please visit the following link :

PHP DATE FUNCTION

Upvotes: 3

Related Questions