简体   繁体   中英

How can i change the date format when echoing a database field?

I have an html input field with a php echo in it. The echo reads from a database field with a timestamp. I need to change the format of the outputted date.

The database has the default 'datetime' sql field and format.

How can i change this to dd/mm/yyyy hh:mm?

I have tried reading the docs on changing a date via php but because of the variable and database field, i dn't seem to be able to formulate it correctly.

Here is an example of a field i'm trying to change:-

<?php echo $update->micp_edit_TIMESTAMP; ?>

You can select the value from database with proper format if you want. Let's try like this way using DATE_FORMAT()

SELECT DATE_FORMAT(your_column_name, '%d/%m/%Y %H:%i') FROM your_tablename

Quick and dirty

//asumming this is a DATETIME field in your database
echo date('d/m/Y H:i', strtotime($update->micp_edit_TIMESTAMP)); 

If you use MySQL use @Curious_Mind 's answer

As per php manual:

Object oriented style

<?php
    $date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
    echo $date->format('Y-m-d');
?>

Procedural style

<?php
    $date = date_create_from_format('j-M-Y', '15-Feb-2009');
    echo date_format($date, 'Y-m-d');
?>

The above examples will output:

2009-02-15

See: http://php.net/manual/en/datetime.createfromformat.php

You can adapt the examples to use the formats you have and require by looking in the manual which explains the format strings. And of course substitute the variables by your own variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM