/    Sign up×
Community /Pin to ProfileBookmark

Problem with Date fuction and a mySQL timestamp

I’m trying to format a date to make it a bit more user friendly, but regardless of the timestamp I use, it outputs the same wrong date. Here is part of the line which is giving me issues:

[CODE]date(“j M Y G:i:s”, $row[‘TS’]) .”</td><td>”.$row[‘TS’][/CODE]

TS is of course my timestamp field retrieved from mySQL. Here is a sample of what I am getting back:

31 Dec 1969 19:33:29 2009-08-20 15:03:36

I actually googled the date yesterday, found a post totally unrelated but had the same TS down to the second. The post said the date I’m getting back is code for an invalid timestamp. I don’t know if that’s true or not.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 21.2009 — The second argument to date() must be a UNIX timestamp integer. Your query is returning a date string (e.g. "2009-08-20 12:34:56"). You can convert that string to a UNIX timestamp integer via strtotime().
[code=php]
date('j M Y G:i:s', strtotime($row['TS']))
[/code]

The reason you are getting the date you are seeing is that a UNIX timestamp integer of 0 (zero) is 1970-01-01 00:00:00 GMT. The time you are seeing is based on the date string you are giving it after it is cast to an integer, probably 2009 (the point where the PHP parser sees a non-integer character). 2009 seconds into the "UNIX era" is 1970-01-01 00:33:29 GMT. Presumably your server's locale setting has it in a timezone which is 5 hours behind GMT (e.g. US CDT or US EST), thus the Dec. 31 date/time.
Copy linkTweet thisAlerts:
@MindzaiAug 21.2009 — You could also convert the date string in your query itself with the MySQL UNIX_TIMESTAMP() function.
×

Success!

Help @coreilly spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...