...

View Full Version : datetime into 12-hour clock



markman641
08-25-2011, 05:17 AM
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:
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?

Nightfire
08-25-2011, 05:33 AM
Take a look at date_format() (http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format)

markman641
08-25-2011, 06:16 AM
ok i looked, how do i implement that? '%c %e %y %r'

Nightfire
08-25-2011, 06:18 AM
Yep, just add the slashes etc wherever you want them

markman641
08-25-2011, 06:25 AM
i know that, but how would i put it in my code?

Nightfire
08-25-2011, 06:31 AM
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

markman641
08-25-2011, 06:37 AM
<?
$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

Nightfire
08-25-2011, 06:43 AM
$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.

markman641
08-25-2011, 06:45 AM
its just showing blank. do i have to change the way its inputted and/or my database? i have the string set as datetime?

kbluhm
08-25-2011, 11:14 AM
http://www.php.net/date
http://www.php.net/strtotime


$datetime = '2011-08-25 02:09:29 ';

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

markman641
08-25-2011, 10:13 PM
now where would i put that? the insert page or the result page? where on there?

markman641
08-25-2011, 10:59 PM
got it to work. ended up doing:


<?
$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>
<? }?>

markman641
08-26-2011, 04:09 AM
it seems like the times are off by 4 hours, how do i fix that?

rose_developer
08-26-2011, 11:24 AM
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')");

?>


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:
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?

markman641
08-27-2011, 07:26 AM
not what i was asking at all, its just that the hours are off

kbluhm
08-27-2011, 03:47 PM
Yes, it is one solution to your issue.

The point of that suggestion was to allow PHP to set the value of the datetime, as opposed to MySQL and NOW(), since MySQL and PHP appear to be offset by 4 hours.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum