Is there anyone sitting on a PHP function to convert a date of format 0000-00-00 00:00:00(datetimesql) to unix timestamp?
            Asked
            
        
        
            Active
            
        
            Viewed 5.3k times
        
    6 Answers
43
            
            
        To do this in PHP 5.2 you can do this:
$datetime = new DateTime();
echo $datetime->format('U');
A newer way to do this as of PHP 5.3 is:
$datetime = new DateTime();
echo $datetime->getTimestamp();
 
    
    
        Abela
        
- 1,225
- 3
- 19
- 42
 
    
    
        John Conde
        
- 217,595
- 99
- 455
- 496
25
            
            
        Another option as you have tagged this question with SQL: the MySQL functions FROM_UNIXTIME and UNIX_TIMESTAMP -> MySQL manual
SELECT UNIX_TIMESTAMP(datetime_column) FROM table
This usually is faster than a PHP function call.
 
    
    
        wvanbergen
        
- 2,294
- 15
- 15
- 
                    I was asking myself the same question... :-) Always try to get the data out of the database as well as possible, as MySQL will always be faster than PHP. – wvanbergen Jan 18 '09 at 17:59
- 
                    I don't think the SQL stuff was in the question when I downvoted this - maybe the edit was during the 5 minute window where it doesn't appear in the edit history? Down-vote reversed. – Alnitak Jan 18 '09 at 18:52
- 
                    1not that the user specified MySQL, and the UNIX_TIMESTAMP() function is not a standard SQL function... – Alnitak Jan 18 '09 at 18:53
- 
                    Alnitak has a valid point, there are a lot of assumptions concerning whether a DB is involved or not wvanbergen. What if he's grabbing timestamps from filenames? – dcousineau Jan 18 '09 at 20:00
- 
                    Timestamps from files already are in unix timestamp format when using PHP functions, the question is tagged SQL and the presented date format is exactly like MySQL returns datetime values. I am not saying this is the only options or the best option, just that it is another approach. – wvanbergen Jan 19 '09 at 02:06
- 
                    1No. No. Don't generate a call to the SQL server just to convert data. If you need this as part of a query - ok, but getting this trivial information out of process is BAD BAD BAD. – elcuco Jan 30 '13 at 09:49
9
            
            
        @bartek - As you noted, PHP's strtotime function is perfect for this. It can handle most common date formats, including strings like "tomorrow" or "+5 months".
 
    
    
        ceejayoz
        
- 176,543
- 40
- 303
- 368
- 
                    2Note that because it can handle so many formats, it is really slow. Do not use it if you are going to call it often. – wvanbergen Jan 18 '09 at 17:58
- 
                    yup, in fact the manual doesn't actually say whether this particular date-time format is supported. That's why I recommended strptime() instead. – Alnitak Jan 19 '09 at 00:46
- 
                    I actually wrapped a website around the strtotime() function in case it helps anyone: www.unixstamp.com – soulkphp Aug 24 '11 at 04:32
6
            
            
        I have a solution for you right here:
/* From MySQL datetime to Unix Timestamp 2003-12-30 23:30:59 -> 1072834230 */
function convertMysqlDateTimeToUnixTimeStamp($date) {
    $yr=strval(substr($date,0,4));
    $mo=strval(substr($date,5,2));
    $da=strval(substr($date,8,2));
    $hr=strval(substr($date,11,2));
    $mi=strval(substr($date,14,2));
    $se=strval(substr($date,17,2));
    return mktime($hr,$mi,$se,$mo,$da,$yr);
}
 
    
    
        devasia2112
        
- 5,844
- 6
- 36
- 56
5
            
            
        Use strptime() to parse the time and turn it into a structured array.
Then pass the results of that into the mktime() function to get a UNIX timestamp.
 
    
    
        Alnitak
        
- 334,560
- 70
- 407
- 495
- 
                    dunno - I'm 99% sure the question didn't mention SQL when I posted this answer – Alnitak Jan 18 '09 at 19:58
-3
            
            
        Encapusulating wvanbergen's solution into a function (for your convenience)
//Convert SQL datetime to unixtime -- by johnboiles
function datetimeToUnixtime($datetime){
    $result = mysql_query("select unix_timestamp('$datetime')");
    $unixtime = mysql_fetch_array($result);
    return $unixtime[0];
}
 
    
    
        johnboiles
        
- 3,494
- 1
- 32
- 26
- 
                    this implies going to the database twice. wvanbergen's solution was meant to be applied to the where the data is fetched from the db in the first place. – Stefan Paul Noack May 14 '12 at 14:03
