To display Timestamp nicely

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeiman
Forum Newbie
Posts: 8
Joined: Wed Jul 17, 2002 5:24 am
Contact:

To display Timestamp nicely

Post 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: :?:
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

strftime()!
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post 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
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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;
}
?>
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

Post 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 
?>
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

Post by sinewave »

Oh man!
I never realized you could do a SELECT *, DATE_FORMAT()

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