0

In my mysql database dateTime is stored in the following form.

00000-00-00 00:00:00

In my php I want to convert it to tiemStamp form like this.

136716425

I tried using

$date2->getTimestamp();

without success, what function should I use to change the format to timestamp?


$sql = "SELECT * FROM temp_user";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {

    $date1 = new DateTime();
    $date2 = $row[dateTime];

    echo $date1->getTimestamp();
    echo $date2->getTimestamp();



}
2
  • duplicate Commented Apr 28, 2013 at 16:01
  • That solution doesn't work in this situation. >< Commented Apr 28, 2013 at 16:07

1 Answer 1

4

MySQL has a builtin function for that called UNIX_TIMESTAMP

SELECT UNIX_TIMESTAMP(NOW())

UPDATE

$sql = "SELECT *,UNIX_TIMESTAMP(dateTime) unx FROM temp_user";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) 
{
    $var = $row['unx'];
    // other codes
}
Sign up to request clarification or add additional context in comments.

1 Comment

Have updated with a code sample. Not sure how I would integrate what you have posted here into that code, as I'm selecting more then just date from SQL.

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.