How to get unix timestamp from a DateTime object using PHP 5.2?

I am working on a multi-year codebase and uses DateTime. The server uses PHP 5.2 .

I see that after PHP 5.3 DateTime :: getTimestamp () was added.

Is it possible to get timestamp from DateTime in PHP 5.2 ?

I used get_class_methods to find out if this method is available, but it is not.

Array ( [0] => __construct [1] => format [2] => modify [3] => getTimezone [4] => setTimezone [5] => getOffset [6] => setTime [7] => setDate [8] => setISODate ) 
+4
source share
3 answers
 $datetime = new DateTime(); echo $datetime->format('U'); 

Look in action

change

As in PHP 5.4, you can do this in one layer:

 echo (new DateTime())->format('U'); 
+10
source

Using U as a parameter to DateTime :: format () is an alternative when using PHP 5.2.

 $ts = $datetimeObj->format('U'); 
+3
source

If you look at manual :

Using U as a parameter to DateTime :: format () is an alternative when using PHP 5.2.

So, DateTime::format('U') is this.

+2
source

All Articles