Online Shopping : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

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

Entry

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:
<?php
// 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.
http://www.tantofa.com
http://www.fantofa.com
http://www.mantofa.com
http://www.tanpola.com
http://www.tampola.com
http://www.mozmar.com
http://www.yamot.com
http://www.templatestemp.com