如何将时间戳声明为变量并转换为ddmmyy格式

时间:2009-09-16 11:22:04

标签: php mysql timestamp

我这里有个约会问题。我在mysql表中有一个名为filedate的时间戳字段。但是我已多次尝试声明它并将其转换为dd-mm-yy格式。不幸的是,当我加载它时,日期仍然是时间戳格式,当我上传它声称我没有声明变量归档。

任何指示我正确方向的协助都将被感激地接受。 所有代码都在下面。我还有一个HTML文件来显示上传和说明。

if (isset($_POST['action']) and $_POST['action'] == 'upload')
{
    // Bail out if the file isn't really an upload
    if (!is_uploaded_file($_FILES['upload']['tmp_name']))
    {
        $error = 'There was no file uploaded!';
        include $_SERVER['DOCUMENT_ROOT'] . 'mediamouse/includes/error.html.php';
        exit();
    }
    $uploadfile = $_FILES['upload']['tmp_name'];
    $uploadname = $_FILES['upload']['name'];
    $uploadtype = $_FILES['upload']['type'];
    $uploaddesc = $_POST['desc'];
    $uploaddata = file_get_contents($uploadfile);
    $filedate = time();


    include 'db.inc.php';

    // Prepare user-submitted values for safe database insert
    $uploadname = mysqli_real_escape_string($link, $uploadname);
    $uploadtype = mysqli_real_escape_string($link, $uploadtype);
    $uploaddesc = mysqli_real_escape_string($link, $uploaddesc);
    $uploaddata = mysqli_real_escape_string($link, $uploaddata);


    $sql = "INSERT INTO filestore SET
            filename = '$uploadname',
            mimetype = '$uploadtype',
            description = '$uploaddesc',
            filedata = '$uploaddata'";
    if (!mysqli_query($link, $sql))
    {
        $error = 'Database error storing file!';
        include $_SERVER['DOCUMENT_ROOT'] . 'mediamouse/includes/error.html.php';
        exit();
    }

    header('Location: .');
    exit();
}

if (isset($_GET['action']) and
        ($_GET['action'] == 'view' or $_GET['action'] == 'download') and
        isset($_GET['id']))
{
    include 'db.inc.php';

    $id = mysqli_real_escape_string($link, $_GET['id']);

    $sql = "SELECT filename, mimetype, filedata, DATE_FORMAT(STR_TO_DATE( filedate, '%d-%b-%Y'), '%Y.%m.%d')
            FROM filestore 
            WHERE id = '$id'";
    $result = mysqli_query($link, $sql);
    if (!$result)
    {
        $error = 'Database error fetching requested file.';
        include $_SERVER['DOCUMENT_ROOT'] . 'mediamouse/includes/error.html.php';
        exit();
    }

    $file = mysqli_fetch_array($result);
    if (!$file)
    {
        $error = 'File with specified ID not found in the database!';
        include $_SERVER['DOCUMENT_ROOT'] . 'mediamouse/includes/error.html.php';
        exit();
    }

    $filename = $file['filename'];
    $mimetype = $file['mimetype'];
    $filedata = $file['filedata'];
    $disposition = 'inline';

    if ($_GET['action'] == 'download')
    {
        $mimetype = 'application/octet-stream';
        $disposition = 'attachment';
    }

    // Content-type must come before Content-disposition
    header("Content-type: $mimetype");
    header("Content-disposition: $disposition; filename=$filename");
    header('Content-length: ' . strlen($filedata));
    echo $filedata;
    exit();
}

if (isset($_POST['action']) and $_POST['action'] == 'delete' and
        isset($_POST['id']))
{
    include 'db.inc.php';

    $id = mysqli_real_escape_string($link, $_POST['id']);

    $sql = "DELETE FROM filestore
            WHERE id = '$id'";
    if (!mysqli_query($link, $sql))
    {
        $error = 'Database error deleting requested file.';
        include $_SERVER['DOCUMENT_ROOT'] . 'mediamouse/includes/error.html.php';
        exit();
    }

    header('Location: .');
    exit();
}

include 'db.inc.php';

$sql = 'SELECT id, filename, mimetype, description, filedate
        FROM filestore';
$result = mysqli_query($link, $sql);
if (!$result)
{
    $error = 'Database error fetching stored files.';
    include $_SERVER['DOCUMENT_ROOT'] . 'mediamouse/includes/error.html.php';
    exit();
}

$files = array();
while ($row = mysqli_fetch_array($result))
{
    $files[] = array(
            'id' => $row['id'],
            'filename' => $row['filename'],
            'mimetype' => $row['mimetype'],
            'description' => $row['description'],
            'filedate' => $row['filedate']);
}

include 'files3.html.php';
?>

2 个答案:

答案 0 :(得分:2)

插入时使用NOW()。格式化时,我认为您不需要解析TIMESTAMP类型列,因为DATE_FORMAT应该能够直接使用时间戳。

这有效:

SELECT DATE_FORMAT( CURRENT_TIMESTAMP( ) , '%Y.%m.%d' )

答案 1 :(得分:0)

您必须在select语句中使用UNIX_TIMESTAMP()