timestamping

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
tiresome
Forum Newbie
Posts: 16
Joined: Sun Apr 18, 2004 9:45 am

timestamping

Post by tiresome »

This is how I created my table in MySql


create table guestBook
(
entry_id integer not null auto_increment,
name varchar(40) null,
location varchar(40) null,
email varchar(40) null,
url varchar(40) null,
comments text null,
created timestamp,
remote_addr varchar(20) null
, key guestbook_key (entry_id)
);

The problem is the created part(ie. timestamp) is displayed like this:

20040420214302

I am actually displaying the records using PHP. Is there a way of formatting the timestamp so that it's more readable?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Change the mysql field to int(10) and you can then store the unix epoch time - number of seconds since 1970 - time(); - You can then call the value from the database and do many things with it using the pre-defined php functions.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

like this

Code: Select all

echo date("l dS of F Y h:i:s A", $timestamp);
Look here on how to format it to your exact needss - http://se.php.net/manual/en/function.date.php

Mark
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

kettle_drum wrote:Change the mysql field to int(10) and you can then store the unix epoch time - number of seconds since 1970 - time(); - You can then call the value from the database and do many things with it using the pre-defined php functions.
...or set it to timestamp data type
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

dont forget the DATE_FORMAT function in mysql =)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

liljester wrote:dont forget the DATE_FORMAT function in mysql =)
You can't use DATE_FORMAT with timestamps as far as i am aware!?

Mark
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

or you can pull stuff with it using substr() - look it up at php.net
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

magicrobotmonkey wrote:or you can pull stuff with it using substr() - look it up at php.net
What you mean :?

Mark
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Code: Select all

<?php
 $year = substr("20040420214302", 0, 4);
 $month = substr("20040420214302", 4, 2);
 $day = substr("20040420214302", 6, 2);

 echo "$day/$month  $year";
?>
[/php_man]
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

ah, ic.

The number he is using isn't a proper unix timestamp, just the years, month etc concenated together.

Mark
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

I know thats why if he wants to do it like that he has to manually extract data!
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

Bech100 wrote:
liljester wrote:dont forget the DATE_FORMAT function in mysql =)
You can't use DATE_FORMAT with timestamps as far as i am aware!?

Mark
you can, this is how i use time in my sites
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

depends what type of timestamp it is. You can only use it with a MySQL timestamp, not a UNIX timestamp.

Personally, i always use UNIX timestamps

Mark
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

Bech100 wrote:depends what type of timestamp it is. You can only use it with a MySQL timestamp, not a UNIX timestamp.

Personally, i always use UNIX timestamps

Mark
yeah im talking about a MySQL timestamp, i never used to do it this way but i seem to of adopted it for quickness
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

liljester wrote:dont forget the DATE_FORMAT function in mysql =)
...or UNIX_TIMESTAMP
SELECT UNIX_TIMESTAMP(anydatefieldtype);
...then use PHP functions on it. Just a different approach...
Post Reply