我正在嘗試製作雙層博客。我想跟蹤註冊用戶發佈博客時的日期和時間。後來,當博客將在前端公佈的時間在我的博客的時間:2:50 PM/AM及日期時間:08月07日2016年 這裏插入代碼如何在MySQL中插入日期和時間並查看它們
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 PM/AM及日期日期:2016年8月7日
你好,這裏是更新的代碼工作正常。我的問題是如何跳過我的.csv文件的第一行,其中通常包含標題(ID,名稱,電子郵件,地址)。
<?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();
}
}
}
}
這就是你說的嗎?此代碼顯示1970年1月1日。 $ time = strtotime($ blog_info ['post_time']); 回聲日期(「M j,Y」,$ time); –
你能告訴我你是如何儲存你的時間嗎?是指存儲在數據庫中的值? –
$ date = date(「M j,Y,g:i a」); $ query =「INSERT INTO tbl_blog(post_time)VALUES($ date)」; 在數據庫中,我創建了一個列名:post_time,輸入:DATETIME,定義如下:當前時間戳 –