3

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

        }
    }

}

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

Is this you are saying? This code display Jan 1 1970. $time=strtotime($blog_info['post_time']); echo date("M j, Y", $time);
can you just tell me how you are storing your time? mean the value that is storing in the database?
$date=date("M j, Y, g:i a"); $query = "INSERT INTO tbl_blog(post_time) VALUES($date)"; In database i create a column name: post_time, type: DATETIME, as-defined: current timestamp
If you are using mysql DATETIME field type then you have to store your custom date in specific format otherwise it will return 1st jan 1970. To solve this situation please change the field type to VARCHAR.
Thanks, this works now. Its showing me UTC time, how can i make this GMT +6?
2

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;

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.