Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert MySQL time to UNIX timestamp using PHP?

There are a lot of questions that ask about 'UNIX timestamp to MySQL time'. I needed the reversed way, yea... Any idea?

like image 637
daGrevis Avatar asked Jan 02 '11 09:01

daGrevis


People also ask

How to convert DateTime to Unix timestamp in PHP?

The Task is to convert a Date to a timestamp using PHP. The task can be done by using the strtotime() function in PHP. It is used to convert English textual date-time description to a UNIX timestamp. The UNIX timestamp represents the number of seconds between a particular date and the Unix Epoch.

What is Unix timestamp in PHP?

Simply put, the Unix timestamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the Unix timestamp is merely the number of seconds between a particular date and the Unix Epoch.

What is Strtotime PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


2 Answers

Use strtotime(..):

$timestamp = strtotime($mysqltime); echo date("Y-m-d H:i:s", $timestamp); 

Also check this out (to do it in MySQL way.)

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp

like image 195
UltraInstinct Avatar answered Sep 24 '22 07:09

UltraInstinct


You can mysql's UNIX_TIMESTAMP function directly from your query, here is an example:

SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19'); 

Similarly, you can pass in the date/datetime field:

SELECT UNIX_TIMESTAMP(yourField); 
like image 36
Sarfraz Avatar answered Sep 23 '22 07:09

Sarfraz