0

Currently I'm using this line of code

echo date("F j Y g:i:s", $row[date]);

But it just gives me January 1 1970 2:33:31

I also want it to look normal because if I don't do the date("F j Y g:i:s", at all, all I get is 2011-03-02 23:00:30 which is the correct date, but displayed in a very abnormal way

1
  • How is that 'date' value stored in MySQL? Integer field? char/varchart? date? datetime? Given what you're getting back from PHP, somehow it's coming back as an integer with value 2131, which is the unix timestamp for Jan 1/70 2:33:31 Commented Mar 3, 2011 at 21:24

2 Answers 2

5

Typecast to UNIX_TIMESTAMP inside the query:

SELECT UNIX_TIMESTAMP(YourDateField) FROM YourTable

Since I see responses to the other answer while this is ignored, let me elaborate:

There are two common date types. Both are actually numbers, that represent a duration since a given date.

  1. One is the number of days (float) since 1900. One is one day. The fraction is the fraction of the day.
  2. Other is the number of seconds since 1970. This can be an int (whole seconds) or a float (including fractional seconds).

If you got the first date, but treat it like the second format, you arecounting days for seconds. Instead of 111 years since 1900, you're counting 111 seconds since 1970. That explains why you get that date.

Therefore, use the UNIX_TIMESTAMP function, which will convert the first float notation to a timestamp in seconds. It is needed because that is also the type PHP uses.

Sign up to request clarification or add additional context in comments.

1 Comment

"UNIX_TIMESTAMP()" returns an epoch date (seconds since 1970), which can be plugged right into "date('format', $val)"
3

You probably need to use strtotime, the date functions expects the 2nd parameter to be a unix timestamp

date("F j Y g:i:s", strtotime($row[date]));

If you want different formatting you can take a look at this page: Date formatting

You might want something like this:

date("Y-m-d H:i:s", strtotime($row[date]));

4 Comments

it just gives me this still: 01/01/70January 1 1970 2:00:00
try var_dump($row[date]) i think your not getting a valid date from your database, maybe you should try $row['date'] (with quotes)
the quotes didnt help, and what do you mean with the var_dump? what does that do?
php > print date("F j Y g:i:s",strtotime('2011-03-02 23:00:30')); > March 2 2011 11:00:30 As you see above strtotime is what you need. Just make sure $row['date'] returns the correct value. Use var_dump($row['data']) to see what is value and type of your variale

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.