How to convert MySQL time to UNIX timestamp using PHP?

There are many questions that ask about the UNIX timestamp for MySQL time. I needed a way back, yes ... Any idea?

+59
php mysql timestamp
Jan 02 '10 at 9:10
source share
5 answers

Use strtotime(..) :

 $timestamp = strtotime($mysqltime); echo date("Ymd H:i:s", $timestamp); 

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

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

+111
Jan 02 '10 at 9:14
source share

You can directly use mysql UNIX_TIMESTAMP directly from your query, here is an example:

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

Similarly, you can go to the date / datetime field:

 SELECT UNIX_TIMESTAMP(yourField); 
+48
Jan 02 '10 at 9:13
source share

From one of my other posts, getting unixtimestamp:

 $unixTimestamp = time(); 

Convert to mysql datetime format:

 $mysqlTimestamp = date("Ymd H:i:s", $unixTimestamp); 

Getting some mysql timestamp:

 $mysqlTimestamp = '2013-01-10 12:13:37'; 

Convert it to unixtimestamp:

 $unixTimestamp = strtotime('2010-05-17 19:13:37'); 

... comparing it with one or more times to find out if the user has been entered in a realistic time:

 if($unixTimestamp > strtotime("1999-12-15") && $unixTimestamp < strtotime("2025-12-15")) {...} 

Unix timestamps are also more secure. You can do the following to validate the passed URL variable before checking (for example) a previous range check:

 if(ctype_digit($_GET["UpdateTimestamp"])) {...} 
+7
Jan 10 '13 at 14:46
source share
 $time_PHP = strtotime( $datetime_SQL ); 
+5
Nov 09 '12 at 16:12
source share

A bit short may be ...

 echo date("Ymd H:i:s", strtotime($mysqltime)); 
+1
Jul 18 '14 at 6:51
source share



All Articles