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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post

    datetime into 12-hour clock

    hello, im using the datetime NOW() function to insert the time into my databases, but it puts it in a strange format, it outputs
    2011-08-25 02:09:29
    when it would be a lot better if it outputted
    8/25/11 7:00 PM
    How would I do that? this is my code:
    Code:
    mysql_query("INSERT into completed (username, name, rate, time) VALUES ('$snuid', '$campname', '$points', NOW())");
    and if the only way to do that is to output it into separate columns, what would i put in the values?

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Take a look at date_format()

  • #3
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    ok i looked, how do i implement that? '%c %e %y %r'

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Yep, just add the slashes etc wherever you want them

  • #5
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    i know that, but how would i put it in my code?

  • #6
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    If you have datetime in the mysql field, that's the only format you can use in that field.
    You can only format it on the select query, which is shown on the link I gave

    SELECT DATE_FORMAT('time','%c %e %y %r') FROM blah

  • #7
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    Code:
    <?
    				$get_name = mysql_query("SELECT DATE_FORMAT('time','%c/%e/%y %r') `username`, `name`, `rate` FROM `completed` WHERE username='$usr'");
     while($namerow = mysql_fetch_array($get_name)) {
    	$date = $namerow["time"];
      	$username = $namerow["username"];
      	$offername = $namerow["name"];
    	$amount = $namerow["rate"];
    ?>
    doesnt work, sorry im really confused idk where to put anything
    Last edited by markman641; 08-25-2011 at 06:41 AM.

  • #8
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    PHP Code:
    $get_name mysql_query("SELECT DATE_FORMAT('time','%c/%e/%y %r') as time, `username`, `name`, `rate` FROM `completed` WHERE username='$usr'"); 
    Showing the error helps, instead of 'doesn't work'.

    You missed a comma, and I thought I'd make it a bit easier to follow by adding the 'as time' in too. That should work now.

  • #9
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    its just showing blank. do i have to change the way its inputted and/or my database? i have the string set as datetime?
    Last edited by markman641; 08-25-2011 at 06:54 AM.

  • #10
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    http://www.php.net/date
    http://www.php.net/strtotime
    PHP Code:
    $datetime '2011-08-25 02:09:29 ';

    echo 
    date'n/j/y, g:i A'strtotime$datetime ) ); // 8/25/11 2:09 AM 

  • #11
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    now where would i put that? the insert page or the result page? where on there?
    Last edited by markman641; 08-25-2011 at 10:23 PM.

  • #12
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    got it to work. ended up doing:

    Code:
    <?
    				$get_name = mysql_query("SELECT `time`, `username`, `name`, `rate` FROM `completed` WHERE username='$usr'");
     while($namerow = mysql_fetch_array($get_name)) {
      	$username = $namerow["username"];
      	$offername = $namerow["name"];
    	$amount = $namerow["rate"];
    	$datetime = $namerow["time"];
    
    
    
    ?>
    
    
    <tr>
    <td><?=$offername?></td>
    <td><?=$amount?></td>
    <td><? echo date( 'n/j/y, g:i A', strtotime( $datetime ) ); ?></td>
    </tr>
    <? }?>

  • #13
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    it seems like the times are off by 4 hours, how do i fix that?

  • #14
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hi,

    try this,
    <?php

    $mytime = date("m/d/Y h:i:s",time());

    mysql_query("INSERT into completed (username, name, rate, time) VALUES ('$snuid', '$campname', '$points', '$mytime')");

    ?>

    Quote Originally Posted by markman641 View Post
    hello, im using the datetime NOW() function to insert the time into my databases, but it puts it in a strange format, it outputs when it would be a lot better if it outputted

    How would I do that? this is my code:
    Code:
    mysql_query("INSERT into completed (username, name, rate, time) VALUES ('$snuid', '$campname', '$points', NOW())");
    and if the only way to do that is to output it into separate columns, what would i put in the values?

  • #15
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    not what i was asking at all, its just that the hours are off


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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