Convert a date to a timestamp?

5

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

$fecha = "2015-12-06";

echo TIMESTAMP($fecha);
    
asked by tomloprod 06.12.2015 в 10:17
source

2 answers

6

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


FORM # 1: Structured style

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


FORM # 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 (class DateTime ).


You can see an interesting benckmark of these two ways to get the timestamp here:
link


My original answer: link

    
answered by 06.12.2015 / 10:17
source
3

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
    
answered by 06.12.2015 в 11:55