Convert a date to timestamp?
How can I get in PHP a date in format of timestamp
?
$fecha = "2015-12-06";
echo TIMESTAMP($fecha);
2 answers
You can use one of the following two ways to get the timestamp
:
SHAPE #1: structured style
// Usa el método strtotime()
$timestamp = strtotime($fecha);
Shape #2: object-oriented style
// DateTime class.
$date = new DateTime($fecha);
$timestamp = $date->getTimestamp();
NOTE ABOUT PERFORMANCE:
The structured style ( method
strtotime()
) is more efficient than the object-oriented style ( classDateTime
).
You can see an interesting benckmark in these two ways 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
If you want to save text in $fecha
Timestamp format, use the strtotime()
function. Once you save in a variable the result of the function with your date as a parameter, you can manipulate it like any timestamp. So to get your date variable with Timestamp format would be like this:
$fecha="2015-12-06";
$variableTimestamp=strtotime($fecha);
Once converted to $variableTimestamp
, you can manipulate it as a timestamp, such as to change the print format. Example:
echo date("d/m/Y", $variableTimestamp);
//Resultado: 06/12/2015