Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 735
Ghost Avatar asked May 19 '26 08:05

Ghost


1 Answers

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/

like image 146
tomloprod Avatar answered May 21 '26 23:05

tomloprod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!