Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Converting a british date to a timestamp

    Hi,

    At the moment I have some dates stored in my database as a string in british format (DD/MM/YYYY). I need to convert these dates into timestamps and then put them back into the database.

    Converting them is easy enough using mktime() which turns this into a Unix Timestamp. Inserting that just puts in todays timestamp by default. I tried using the date() function to create a timestamp in mysql format (which I believe is YYYYMMDDhhmmss) but this didn't work either. If anybody could help me, that'd be awesome. Anyway, here's some of the code I was using to try and get this to work.

    PHP Code:
    $date explode("/",$articleResult['DateAdded']);

    $newDate mktime(0,0,0,$date[1],$date[0],$date[2]);
            
    $newDate2 date("YmdHis"$newDate);
        
    mysql_query("UPDATE new_article SET DateAdded2 = '".$newDate2."' WHERE ArticleID = '".$articleResult['ArticleID']."'"); 

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,020
    Thanks
    2
    Thanked 313 Times in 305 Posts
    Use the mysql STR_TO_DATE() function directly in a single UPDATE query - http://dev.mysql.com/doc/refman/5.0/...on_str-to-date

    There is no need to select the data, loop through it, convert it, and UPDATE the DateAdded2 column. Just UPDATE the DateAdded2 column directly.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •