Online Shopping : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to
Did You Find This Entry Useful?

2 of 19 people (11%) answered Yes
Recently 2 of 10 people (20%) answered Yes


How do I format a timestamp(length 8) date into YYYY-MM-DD when I display them with echo function?

Mar 15th, 2008 17:57
ha mo, Michael Foss, Penton Wong,

I would assume you are talking about a MySQL TIMESTAMP(8) data type. 
If so, the following code will work:
// Assuming a $timestamp_MySQL variable
// has been populated with a TIMESTAMP(8)
// cell from MySQL.
$year = substr($timestamp_MySQL, 0, 4);
$month = substr($timestamp_MySQL, 4, 2);
$day = substr($timestamp_MySQL, 6, 2);
echo "$year-$month-$day";
The three substr calls seperate the variable $timestamp_MySQL, which 
is in YYYYMMDD format, into 3 strings that can easily be worked with.