5

In MySQL:

select UNIX_TIMESTAMP('2009-12-08 21:01:33') ;

I need to get the value from PHP.

4 Answers 4

18
echo time();

or

echo strtotime('now');

strtotime will:

Parse about any English textual datetime description into a Unix timestamp

So you might just try:

echo strtotime('2009-12-08 21:01:33');
Sign up to request clarification or add additional context in comments.

1 Comment

But unfortunately UNIX_TIMESTAMP('0000-00-00 00:00:00') !== strtotime('0000-00-00 00:00:00')
1

strtotime()

Comments

0
mktime(21, 1, 33, 12, 8, 2009); // mktime($hours, $minutes, seconds, $month, $day, $year)

In addition, mktime() function is also can be used for this purpose. However you need to provide parameters, but an advantage of this function - you can get timestamp for the desired date and time, not only current timestamp.

Also your need to remove '0' from the beginning of the value. In other case these values will be recognized as octal.(08 => 8, 01=>1).

Comments

0

In case that you are not asking which is the equivalent UNIX_TIMESTAMP function in php. But you ask how to convert UNIX_TIMESTAMP() returned value to php DateTime object. Then you you can do it by prepending a "@" in time returned by sql query before passing it at DateTime constructor.

Like this:

$tm = new DateTime('@' . $sqlrow['time']);

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.