PHP equivalent of UNIX_TIMESTAMP () MySQL?

In MySQL:

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

I need to get the value from PHP.

+4
source share
4 answers
 echo time(); 

or

 echo strtotime('now'); 

strtotime will be:

Parsing any text text in English datetime description in Unix timestamp

So you can just try:

 echo strtotime('2009-12-08 21:01:33'); 
+17
source
+1
source
 mktime(21, 1, 33, 12, 8, 2009); // mktime($hours, $minutes, seconds, $month, $day, $year) 

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

You also need to remove "0" from the beginning of the value. Otherwise, these values ​​will be considered octal (08 => 8, 01 => 1).

0
source

If you are not asking which function is equivalent to UNIX_TIMESTAMP in php. But you are asking how to convert the return value of UNIX_TIMESTAMP () to a php DateTime object. You can then do this by adding "@" at the time returned by the SQL query before passing it in the DateTime constructor.

Like this:

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

All Articles