3

I am setting a Database value within from a PHP file. In php file i have a string variable which stores unix timestamp value.

MySql table i am having is having a schema where i have to store these timestamp values in login field which is of timestamp datatype.

i tried sending

date('Y-m-d G:i:s', strtotime($userLogin));

to my database but all it stores is 0000-00-00 00:00:00

1
  • Have you confirmed the value of $userLogin is valid? It's possible that strtodate() is returning FALSE (zero). To debug, you should consider breaking the statement into two steps: 1) assign the return value of strtotime() to a variable so you can validate it, and 2) call date() using that newly created variable. Commented Jul 11, 2012 at 22:23

5 Answers 5

2

MySQL has FROM_UNIXTIME() function for this.

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

Comments

2

MySQL provides the FROM_UNIXTIME( ) and UNIX_TIMESTAMP( ) functions to converta Unix timestamp to a MySQL date format, and vice versa.

Example:

$sql = "INSERT INTO yourtable(date, ..., ...) VALUES (FROM_UNIXTIME($yourdate), ..., ...)";

Comments

1

strtotime () is intended for converting strings in various date formats into UNIX timestamps. If the string is a timestamp already then it won't look like a meaningful formatted date/time to strtotime () and it will fail.

If the string is already a timestamp, then you don't need to do anything more to it than cast it to integer (strictly speaking even that step shouldn't be necessary, but casting will strip out any non-numerical characters, so it doesn't usually hurt to cast)

Additionally, MySQL is capable of parsing UNIX timestamps (with FROM_UNIXTIME(), I think, I'd have to look it up to be sure)

Comments

0

If $userLogin holds timestamp value than you don't need to use strtotime(),

For example, this one should work -->

$userLogin = time();
date('Y-m-d G:i:s', $userLogin);

Comments

0

I had to add

    $login = date('Y-m-d H:i:s',(int)($userLogin/1000));

and it worked...

Comments

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.