How can I get a date in PHP format in timestamp
?
$fecha = "2015-12-06";
echo TIMESTAMP($fecha);
How can I get a date in PHP format in timestamp
?
$fecha = "2015-12-06";
echo TIMESTAMP($fecha);
You can use one of the following two ways to get timestamp
:
// Usa el método strtotime()
$timestamp = strtotime($fecha);
// 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 of these two ways to get the timestamp
here:
link
My original answer: link
If you want to save the text in $fecha
with timestamp format, use the function strtotime()
. Once you save in a variable the result of the function with your date as a parameter, you can manipulate it as any timestamp. Then to obtain your variable date with timestamp format it would be like this:
$fecha="2015-12-06";
$variableTimestamp=strtotime($fecha);
Once converted to $variableTimestamp
, you can manipulate it as timestamp, such as to change the print format. Example:
echo date("d/m/Y", $variableTimestamp);
//Resultado: 06/12/2015