PHP: I need a lil help
- Started
- Last post
- 13 Responses
- ********
I need to echo out a timestamp from my Database... which looks like this;
20040107174524
I want to format it so it looks like this;
2004/01/07
Can that be done?
As you can see... I'm none to bright with this stuff.... and php.net has me lost on this one....
- rabattski&rabattski0
ofcourse you can. gimme a sec to look it up.
- ********0
Thanks.
I got:
At the moment... tried other things... but it broke.
- ********0
Oh... can't do that :(
Try again:
echo $row_display_post['date']
- rabattski&rabattski0
i assume you already extracted the unix timestamp from your database.
either use time or date.
date("Y/m/d",timestamp)
or
time("Y/m/d",timestamp)
just take your time at php.net to understand it. most usefull bits are always in the comments.
http://de.php.net/manual/en/func…
http://de.php.net/manual/en/func…
date("D M j G:i:s T Y");
- ********0
Mmmm. Maybe I'm doing this wrong. I'm getting 1970/01/01- which is wrong!
My field name which contains the TS is "date"... maybe thats bad... or I haven't called it. God this stuff is hard work...
- rabattski&rabattski0
http://perl.about.com/library/ph…
check the example.
my_time would be your timestamp.
and the format you're looking for is Y/m/d (fullyear/month/day)
- dequid0
that won't work because 20040107174524 isnt a real timestamp (epoch seconds).
i would try with it with substr()
- rabattski&rabattski0
hey you're right! didn't even bother to really read that stamp.
- ********0
Oh dear. It's all too confusing. I've basicly changed my "timestamp" to a "date" entry which just echos out as 2004-01-03 etc.... not the format I was after, but it will do...
- rabattski&rabattski0
you can replace those -'s with /'s in a jiffy and you'll have that format you're after (str_replace or preg_replace)
- whiteSneaks0
i was just working on this yesterday. i accomplished it with a query. first pull the date then
SELECT DATE_FORMAT ('$date', '%e %M, %Y);
the date var is 0000-00-00 format the second part formats it however you like
- ashley0
This is the function I created to do the same thing:
function timestamptodate($timestamp){
return substr($timestamp, 4, 2) . "/" . substr($timestamp, 6, 2) . "/" . substr($timestamp, 0, 4);
}
- mike0
nonono...
put the value as the second argument in the date() function [the first argument being the format string] and make sure to run strtotime() on the value out of the db as it's a string by default. do not start doing regex rpelaces and string manipulations.