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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    159
    Thanks
    37
    Thanked 2 Times in 2 Posts

    Exclamation date Format code help while posting info in to mysql

    HI frnds,

    can any one please help me to post dd-mm-yyyy date format to Mysql which is yyyy-mm-dd.


    I am struggling lot to solve this but could not able to success it....

    my php code is :


    Code:
    <?php
    session_start();
    $host="localhost"; // Host name 
    $username="#############"; // Mysql username 
    $password="##########"; // Mysql password 
    $db_name="#########"; // Database name 
    $tbl_name="#########"; // Table name 
    $myusername = $_SESSION['myusername']; //user who updating
    $con = mysql_connect("$host","$username","$password")or die("cannot connect to server");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="INSERT INTO $tbl_name(`Creation_Date`, `Resolved_Date`, `Analyst`, `Email_Address`, `Status`, `Mail_Sent`, `Call_Made`, `Doc_verf`, `Change2analyst`, `Change3analyst`) 
    
    VALUES 
    
    ('$_POST[requiredCD]','$_POST[requiredRD]','$myusername','$_POST[requiredEmail]','$_POST[Status]','$_POST[MailSent]','$_POST[Call]','$_POST[Docverf]','$_POST[Analyst2]','$_POST[Analyst3]')";
    
    $result1=mysql_query($sql)or die(mysql_error());
    {
    header("location:new_user.php") or die("record not inserted");
    }
    mysql_close();
    ?>
    Any help is most appreciated !!!!

    Regards,
    Nani

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Well, since you don't mention which of these it is all I can give is an example. dd-mm-yyyy is valid in php for a date format: http://ca1.php.net/manual/en/datetime.formats.date.php so you can use it in strtotime or dateTime classes.
    PHP Code:
    $sDate '02-02-2012';
    $dt = new DateTime($sDate);
    print 
    $dt->format('Y-m-d'); // 2012-02-02 
    You'll want to look into database security or prepared statements as well.

  • #3
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    159
    Thanks
    37
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Fou-Lu View Post
    Well, since you don't mention which of these it is all I can give is an example. dd-mm-yyyy is valid in php for a date format: http://ca1.php.net/manual/en/datetime.formats.date.php so you can use it in strtotime or dateTime classes.
    PHP Code:
    $sDate '02-02-2012';
    $dt = new DateTime($sDate);
    print 
    $dt->format('Y-m-d'); // 2012-02-02 
    You'll want to look into database security or prepared statements as well.
    Fou-Lu,


    Dont mind can you be more specific with example code I have posted .......I am a beginner and trying to understand a lot but it became tuff for me .... so please help me.

    In the above code I have created a frontend form for the Creation_Date & Resolved_Date which takes dd-mm-yyyy but when i am posting it should post to database as yyyy-mm-dd..

    Regards,
    Nani

  • #4
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    332
    Thanks
    2
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by nani_nisha06 View Post
    HI frnds,

    can any one please help me to post dd-mm-yyyy date format to Mysql which is yyyy-mm-dd.


    I am struggling lot to solve this but could not able to success it....

    my php code is :


    Code:
    <?php
    session_start();
    $host="localhost"; // Host name 
    $username="#############"; // Mysql username 
    $password="##########"; // Mysql password 
    $db_name="#########"; // Database name 
    $tbl_name="#########"; // Table name 
    $myusername = $_SESSION['myusername']; //user who updating
    $con = mysql_connect("$host","$username","$password")or die("cannot connect to server");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="INSERT INTO $tbl_name(`Creation_Date`, `Resolved_Date`, `Analyst`, `Email_Address`, `Status`, `Mail_Sent`, `Call_Made`, `Doc_verf`, `Change2analyst`, `Change3analyst`) 
    
    VALUES 
    
    ('$_POST[requiredCD]','$_POST[requiredRD]','$myusername','$_POST[requiredEmail]','$_POST[Status]','$_POST[MailSent]','$_POST[Call]','$_POST[Docverf]','$_POST[Analyst2]','$_POST[Analyst3]')";
    
    $result1=mysql_query($sql)or die(mysql_error());
    {
    header("location:new_user.php") or die("record not inserted");
    }
    mysql_close();
    ?>
    Use strtotime which converts the time you give it to a unix timestamp which can then be easily coverted to any date format you want using the date() function.

    Like this:
    PHP Code:
    <?php
    //E.g
    $date1 '$_POST['requiredCD']'//$Date variable now has date like 23-09-1994

    $date_tstamp strtotime($date1);

    $date_converted date('Y-m-d'$date_tstamp//Now contains 1994-09-23
    The above could have been done with the below one-line code:
    PHP Code:
    <?php

    $date_converted 
    date('Y-m-d'strtotime($_POST['requiredCD']));  //$date_coverted contains 1994-09-23
    Last edited by Redcoder; 10-15-2012 at 10:16 AM.


  •  

    Posting Permissions

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