Ghost
Ghost

Reputation: 223

Convert date to integer (timestamp)

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);

Upvotes: 3

Views: 16860

Answers (2)

tomloprod
tomloprod

Reputation: 7882

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/

Upvotes: 3

The function you search for is strtotime();.

Upvotes: 0

Related Questions