Page 1 of 1

To display Timestamp nicely

Posted: Thu Sep 12, 2002 5:07 am
by jeiman
I read a date from db in timestamp(14) format(YYYYMMDDHHMMSS). What is the function in php should I use to display the date in nice format such as dd-mm-yyyy hh:mm:ss am/pm. I have tried strtotime, strftime, getDate function but it's not working. Can somebody help me to solve the problem. Thanks guys.... :roll: :?:

Posted: Thu Sep 12, 2002 7:36 am
by Takuma
strftime()!

Posted: Thu Sep 12, 2002 10:33 am
by dusty
does strftime() work with a mysql timestamp? he said he tried it already.

just format your date in your query

Code: Select all

mysql_query("SELECT *, DATE_FORMAT(date_col, '%d-%m-%Y %r') as date_col FROM table");
i believe that's correct, if not search the manual for date_format

Posted: Thu Sep 12, 2002 12:14 pm
by JPlush76
yea dusty is correct, using the date format in the mysql call uses less resources on your server, thats the way I'd go

but if you still wanna use a function you can do this

Code: Select all

<?php
function timedate($date)
{
// start date routine from a timestamp field
$datefromdb = $date;
$year = substr($datefromdb,0,4);
$mon = substr($datefromdb,4,2);
$day = substr($datefromdb,6,2);

$date = $mon."/".$day."/".$year;

return $date;
}
?>

Posted: Thu Sep 12, 2002 3:16 pm
by DSM
I also always save timestamp as a VARCHAR in mysql, makes it easier to deal with later.
I use date() and format however it needs to be formatted for whatever I happen to be doing.

Code: Select all

<?php
where $time = your time column
$day_time = date("F j, Y, g:i a", $time);

prints this format-> August 19, 2002, 5:49 pm 
?>

Posted: Thu Sep 12, 2002 4:57 pm
by sinewave
Oh man!
I never realized you could do a SELECT *, DATE_FORMAT()

i was always putting every column seperated by commas..thanks!