www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-04-2009, 01:36 AM
    tariq_wajiha tariq_wajiha is offline
    Registered User
     
    Join Date: Nov 2008
    Posts: 78
    Insert date&time using php into mysql

    Hello everyone,
    I have a form in which there is create date and publishing date field the date and time is selected from the calender placed next to these text field and is a javascript calender. In database createdate and publishingdate fields type is datetime and the query i am using is given below:

    PHP Code:
    $title = $_POST['title'];
        
    $category_name = $_POST['cat_name'];
        
    $published = $_POST['published'];
        
    $description = $_POST['description'];
        
    $createdate = $_POST['createdate'];
        
    $publishingdate = $_POST['publishingdate'];
        
    $showtitle = $_POST['showtitle'];
        
    $accesslevel = $_POST['accesslevel'];
        
        
    $sql = "INSERT INTO article
        (`title` ,`category_name` ,`published`, `description`, `createdate`, `publishingdate`, `showtitle`, `accesslevel`
    ) values
    ('$title', '$category_name', '$published', '$description', '$createdate', '$publishingdate', '$showtitle', '$accesslevel')"
    ;

    When i fill the form and save it all the input is saved in the database but the datetime is saved with a null value--->0000-00-00 00:00:00.


    One more thing i would like to mention here is when i keep the datatype of createdate and publishing date to text the date and time is stored as they are mentioned in the input field through javascript calender.


    Can anyone help me out how can i insert the datetime when my datatype is datetime.

    Thanks
    Reply With Quote
      #2  
    Old 11-04-2009, 02:36 AM
    scragar's Avatar
    scragar scragar is offline
    Some Random Guy
     
    Join Date: Jun 2003
    Location: here
    Posts: 4,461
    http://dev.mysql.com/doc/refman/5.1/en/datetime.html

    Check the format of the date-time you are attempting to store.
    __________________
    If you are using PHP please use the [php] and [/php] forum tags for highlighting...
    The same applies to HTML and the forums [html][/html] tags.
    Reply With Quote
      #3  
    Old 11-04-2009, 02:43 AM
    tariq_wajiha tariq_wajiha is offline
    Registered User
     
    Join Date: Nov 2008
    Posts: 78
    4-11-2009 12:42:24

    This is how it is picking the date and time, the time is current time of my system
    Reply With Quote
      #4  
    Old 11-04-2009, 03:00 AM
    scragar's Avatar
    scragar scragar is offline
    Some Random Guy
     
    Join Date: Jun 2003
    Location: here
    Posts: 4,461
    Quote:
    As a string in either 'YYYY-MM-DD HH:MM:SS' or 'YY-MM-DD HH:MM:SS' format.
    The answer to your question is right there, the format is not what MySQL accepts.

    Personally I've always used timestamps instead of DATETIME, so much easier to work with.
    Storing/Retrieving with timestamps
    Code:
    function toTimestamp($str){
      list($day, $month, $year, $hours, $mins, $secs) = preg_split('/[^\d/', $str);
      return mktime ($hours, $mins, $secs, $month, $day, $year);
    }
    function fromTimestamp($stamp){
      return date('j-n-Y H:i:s', $stamp);
    }
    Storing/Retrieving with DATETIME
    Code:
    function toDateTime($str){
      list($day, $month, $year, $hours, $mins, $secs) = preg_split('/[^\d/', $str);
      $tmp = mktime ($hours, $mins, $secs, $month, $day, $year);
      return date('Y-m-d H:i:s', $tmp);
    }
    function fromDateTime($dt){
      list($year, $month, $day, $hours, $mins, $secs) = preg_split('/[^\d/', $str);
      $tmp = mktime ($hours, $mins, $secs, $month, $day, $year);
      return date('j-n-Y H:i:s', $tmp);
    }
    You might also want to try playing around with StrToTime, although I do find it slower.
    __________________
    If you are using PHP please use the [php] and [/php] forum tags for highlighting...
    The same applies to HTML and the forums [html][/html] tags.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 07:51 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.