php – Convert a date to timestamp?

Question:

How can I get a date in timestamp format in PHP?

$fecha = "2015-12-06";

echo TIMESTAMP($fecha);

Answer:

You can use one of the following two ways to get the timestamp :

WAY # 1: Structured Style

// Usa el método strtotime()
$timestamp = strtotime($fecha);

WAY # 2: Object Oriented Style

// DateTime class.
$date = new DateTime($fecha);
$timestamp = $date->getTimestamp();

NOTE ABOUT PERFORMANCE:

Structured style ( strtotime() method) is more efficient than object-oriented style ( DateTime class).

You can see an interesting benchmark of these two ways to get the timestamp here:
http://en.code-bude.net/2013/12/19/benchmark-strtotime-vs-datetime-vs-gettimestamp-in-php/


My original answer: https://stackoverflow.com/a/28406427/4359029

Scroll to Top