0

Possible Duplicate:
Formating an SQL timestamp with PHP

I have a column in my SQL table that has default current timestamp values. I am trying to output it in a more readable format using date().

However, no matter which format I use, I always get the same date: 1970-01-01 00:33:32

Here is an example value of the current timestamp from the DB: 2012-08-09 06:37:58

Below is the code I use to try to "convert" it to a readable format:

$value['current_date']// is the var from the database.(containing 2012-08-09 06:37:58)
$somevar = date('Y-m-d H:i:s', $value['current_date']);

Thanks in advance.

1
  • Thanks for everyone else who helped. Commented Aug 11, 2012 at 2:59

3 Answers 3

1

Your problem is that you are using the wrong value in the second parameter. The date() function expects a UNIX-style timestamp as the second parameter, not a string representation of a date. Use the strtotime() function to correct this:

$somevar = date('Y-m-d H:i:s', strtotime($value['current_date']));

As someone else pointed out, however, why are you bothering to do this formatting? The format style you want is already in the database. It should be as easy as:

echo $value['current_date'];
Sign up to request clarification or add additional context in comments.

5 Comments

Jonah Thanks for the help, does TIMESTEMP - current timestemp as a default doesn't use UNIX style ?
TIMESTAMP is a MySQL type; it has (essentially) no correlation to the types present in PHP. The strtotime function will convert the TIMESTAMP value (essentially a string), to the UNIX-timestamp (integer) equivalent.
jonah your idea seemed to work
Great! I'm glad to help out. Remember to mark the answer as accepted, to help others in the future who stumble upon this question.
Wouldn't forget. Thanks again
0

The problem that you are having is because the second argument of the date() should be a timestamp, and since you are passing the raw string containing 2012-08-09 06:37:58, php does not know what to make of it.

Modify your code as below and it should work:

$somevar = date('Y-m-d H:i:s', strtotime($value['current_date']));

You can now use any date formats in place of 'Y-m-d H:i:s' as you wish

Comments

0

First of all: Why formatting the timestamp if it already exists in the desired format?

Regarding the actual problem:

See https://www.php.net/manual/en/function.date.php

date() only accepts unix timestamps (seconds since 1970-1-1 00:00:00 UTC) or no timestamp at all. if you want to work with a timestamp like you have you need to create an unix timestamp first with date_create(), mktime , or strtotime

1 Comment

Leon That was just a test i thought of using something safe after all the tries I've made.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.