views:

685

answers:

4

In MySQL:

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

I need to get the value from PHP.

+9  A: 
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');
karim79
+1  A: 

strtotime()

Amber
A: 
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).

Alex
A: 

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']);