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
    May 2010
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Date saving problem

    hy every one i have two files one is index.php and second insert.php as follow,

    index.php
    Code:
    
    <html>
    <body>
    
    <form action="insert.php" method="post">
    Time <input type="text" name="time" />
    <input type="submit" />
    </form>
    
    <?php
    $ctime = date('h:i:s A');
    echo "Current time is : " . $ctime ;
    echo "<br>";
    
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("time", $con);
    
    $result = mysql_query("SELECT * FROM time");
    
    while($row = mysql_fetch_array($result))
      {
      echo date('h:i:s A', strtotime($row['time']));
      
      echo "<br />";
      }
    
    mysql_close($con);
    ?> 
    
    
    </body>
    </html>
    here is my insert.php file code,

    Code:
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("time", $con);
    
    $sql="INSERT INTO time (time )
    VALUES
    ('$_POST[time]')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added";
    echo "<br>";
    echo "<a href='index.php'>Home</a>";
    
    mysql_close($con)
    ?>

    this code work fine but the form which submit the value of time in database whos code is in index.php file above,
    i have to enter time in the form text field like this 17:00 and it save the value in database but,
    i want to enter time in the form text field like this 05:00 PM which automatically save the value in database as 17:00
    what should i do with the code help me please
    thanks

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The function strtotime() should be just what you want.

    (edit) Correction, I was thinking of the date_parse() function, not strtotime(). strtotime() would give you a unix timestamp, but if you just want the time portion, then try date_parse().
    Last edited by Fumigator; 05-25-2011 at 08:46 PM.


  •  

    Posting Permissions

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