time()

They have: 164 posts

Joined: Nov 2001

if a person he insert register date using time() into the database and data type in database is int(11), how am i suppose to retrieve the date out to something like this, 11-11-2002??

He has: 296 posts

Joined: May 2002

It would require you to make a function. You can't just extract something that is inserted as 11112002 and read it as 11-11-2002.

Something like this:

<?php
$time
= '11112002';
$month=substr(0,2,$time);
$day=substr(2,2,$time);
$year=substr(4,4,$time);

$date = $month.\"-\".$day.\"-\".$year;
echo
$date;
?>

It would change 11112002 to 11-11-2002

More on the SubStr function

[James Logsdon]

They have: 45 posts

Joined: Feb 2002

I would use date, not time personally. And just put it in text, not int.

I'm not quite sure what you're asking though.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I'm assuming you're using mySQL. mySQL has special fields for time data: DATE, DATETIME, TIME, TIMESTAMP, and YEAR. For some reason (probably habit formed from programming in PERL), people like to use the time() function and store it in an INT(11).

If this is the case, you can pull it out like so:
SELECT FROM_UNIXTIME(time_field,'%m-%d-%Y') FROM table_name

If the format is MMDDYYYY, you can pull it out like so:
SELECT CONCAT(SUBSTRING(time_field,1,2),'-',SUBSTRING(time_field,3,2),'-',SUBSTRING(time_field,5,4)) FROM table_name

If the format is YYYYMMDD, or YYYYMMDDHHMMSS, you can pull it out like so:
SELECT DATE_FORMAT(time_field,'%m-%d-%Y') FROM table_name

In the future, I recommend using the appropriate datatype.

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.