1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

timestamp to datetime

Discussion in 'PHP' started by Weirfire, Jun 27, 2006.

  1. #1
    I must have asked this question about 15 times lol
    SEMrush
    How do I convert a PHP timestamp 1128831859 into MySQL datetime format 0000-00-00 00:00:00

    Is it something like

    $datetime = date(Y-m-d h:j:s, $timestamp);


    ??
     
    Weirfire, Jun 27, 2006 IP
    SEMrush
  2. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    
    date("Y-m-d H:i:s", $timestamp);
    
    Code (markup):
     
    Evoleto, Jun 27, 2006 IP
  3. janos

    janos Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $datetime = date("Y-m-d H:i:s", $timestamp);
     
    janos, Jun 27, 2006 IP
  4. ipheo.com

    ipheo.com Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can do it with MySQL and the appropriate date function, FROM_UNIXTIME, that'll spare you the formatting.
     
    ipheo.com, Jun 27, 2006 IP
  5. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,978
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #5
    Thanks but I'm putting it into the format to enter the date into the DB. The above solutions were what I had been looking for.

    I appreciate the help though :)
     
    Weirfire, Jun 27, 2006 IP
  6. rashod

    rashod Greenhorn

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #6
    date("Y-M-d-D H:i", $timestamp);
    This is what im using this will output something like this 2013-Jan-08-Tue 11:40
     
    rashod, Jan 12, 2013 IP
  7. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    110
    #7
    Why would you save the formatted timestamp into MySQL !? Just save it as UNIX integer and make a function to parse it whenever you need it. Let's suppose you may want to work with those dates, you'd be forced to work with strtotime which is not that convenient.
     
    edduvs, Jan 13, 2013 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,051
    Likes Received:
    1,674
    Best Answers:
    236
    Trophy Points:
    515
    #8
    Because the DATATIME data type in SQL allows or a wider date range with no 2039 it's gonna blow up, and is generally easier/faster to deal with when building queries -- particularly if you are going to use SQL functions like DATE_ADD with things like INTERVAL and metrics like DAY or YEAR.

    Letting you keep PHP as the glue and having mySQL do the heavy lifting, instead of brute-forcing things the hard way by dicking around with unix time.

    Which is probably why if possible I'd use now() or DATE_ADD( NOW(), expression ) instead of trying to copy a *nix timestamp over from PHP.
     
    deathshadow, Jan 13, 2013 IP
  9. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    110
    #9
    I think you're underestimating the use of "DATATIME" (which is actually "DATETIME") and the INT(10) field type in MySQL. I prefer using INT(10) and inserting a unix_timestamp() into the MySQL since i'm not interested in indexing it, this way you'll get more than 2039 :) .

    PS. Lean to be less stubborn and arogant because you couldn't know how was I going to use the field :)
     
    edduvs, Jan 13, 2013 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,051
    Likes Received:
    1,674
    Best Answers:
    236
    Trophy Points:
    515
    #10
    Typo -- common when on these POS laptop keyboards instead of the Model M at my workstation.

    While I would prefer not to use something as limited as a unix timestamp given that at 32 bits it's blowing up in few decades (though if you're on 64 bit that's a non-issue)... But my big point though is why send a timestamp from your PHP when you could just have mySQL use it's own internally? One that you can perform relational queries on with ease? That way you don't have to play with your dates at all from the PHP -- let the query do the work. In my own code I've stopped passing PHP dates to anything that is just pulling the current time and/or is relational to it... it just makes sense when mySQL has a data format for dates, the ability to internally perform math on dates, and the ability to do actions like:

    
    	'addUserSession' => '
    		INSERT INTO !prefix!sessions (
    			sessionId,userId,expires,sessionTime,ipAddress,userAgent
    		) VALUES (
    			:sessionId,
    			:userId,
    			DATE_ADD(NOW(), INTERVAL :expires SECOND),
    			:expires,
    			:ipAddress,
    			:userAgent
    		)
    	',
    	
    	'updateUserSessionExpire' => '
    		UPDATE !prefix!sessions
    		SET expires = DATE_ADD(NOW(), INTERVAL sessionTime SECOND)
    		WHERE sessionId = :sessionId;
    	',
    
    	'purgeExpiredSessions' => '
    		DELETE FROM !prefix!sessions
    		WHERE expires < NOW();
    	',
    
    
    Code (markup):
    Funny, I thought this was about how the OP was going to use it --not you! ...and your asking why they'd want to use it that way. But of course answering your question is somehow being "arrogant" and "stubborn" -- RIGHT.

    Really though, we don't know how Weirfire was going to use it or what the date source is -- again why without code, an explanation of what it's being used for, etc, etc -- we're all guessing wildly in the dark.

    But we can make that an educated guess since they did ask about DATETIME... so one would assume they were using it for the reasons I outlined.
     
    Last edited: Jan 13, 2013
    deathshadow, Jan 13, 2013 IP
  11. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    110
    #11
    I guess this is the time when I should argue with you and create a smartass-wiseman loop (especially those you're trying to get me into ?)
     
    edduvs, Jan 15, 2013 IP