简体   繁体   中英

how to format date coming from MYSQL

Simple question but i need it :)

  $query = "SELECT * 
FROM  `set` 
WHERE ID = '$id'";

$result = mysql_query($query);
$info = mysql_fetch_assoc($result);

$frmdate = $info['date'];

So far i tried

$timestamp = strtotime($info['date']);
$joined_date = date("j M. Y", $timestamp);

but no luck !

"date" field in mysql is DATE data type

I want to make it like "23 Oct. 2009"

how can i do it ?

Thanks

You can use strtotime to convert the string representation of the date into a timestamp then you can use date to convert the timestamp into whatever format you like:

$timestamp = strtotime($frmdate);
$formatted_date = date("j M. Y", $timestamp);

You can also move the translation from the yyyy-mm-dd format to timestamp into the query itself, using:

select unix_timestamp(field_name) as field_name_timestamp

You could format the date in the MySQL database server.

SELECT  *, DATE_FORMAT( date, '%e %b. %Y' ) AS formatted_date
FROM    `set` 
WHERE   ID = '$id'

Then you would just need to modify your client php code to just use the string returned by the database call in $info['formatted_date'] . Use 'd' in place of 'e' in the format string if you do not want a leading zero on the day of month.

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