Convert date to integer (timestamp)

14,326

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

METHOD #1: Structured style.

// Use the strtotime() function
$timestamp = strtotime($Date1);

METHOD #2: Object-oriented style.

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

NOTE ABOUT PERFORMANCE:

The structured style ( strtotime() ) is faster than the Object-oriented style ( DateTime ).


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

Share:
14,326
Ghost
Author by

Ghost

Updated on June 04, 2022

Comments

  • Ghost
    Ghost almost 2 years

    In mysql , the data type is integer... And I want to save date as 1422104820 format (timestamp).

    $Date = $_POST['Date'];
    $Time = $_POST['myTime'];
    $Date1 = $Date.$myTime;
    
    insert into table ('date') values ($Date1);