Hi, What is the correct format for inserting the current time into MySQL? Basically I want a 24 hour time to be stored in the db and when outputted through php I get something like: 13:46. thanks in advance, Keith
You can see some examples of adding and displaying time in mysql on the page tizag.com/mysqlTutorial/mysql-time.php
Output can be formatted however you want using the date() function But checking out php.net for the date func showing you all the options is a good idea. In regards to storing time in the db. I would store everything in seconds. Basically strtotime. Because then you can do this: <? $mydate=strtotime("now"); $mydate=date("m/d/Y", $mydate); echo "$mydate"; ?> Code (markup): If you ran that today the output would be 01/07/2008 Storing timestamps in the db in seconds is the way I would do it. It is very easy to do a call back when searching to convert both ways to find a range. Example: $q="select * from table where tstamp >= $first_converted_range and tstamp <= $last_converted_range": Code (markup):
<?php $roflcopterzomg=time(); $roflcopterzomg2=date('Y-m-d'); $explodingcatintree = mysql_query("INSERT INTO `tablehere` (time , date) VALUES ('$roflcopterzomg','$roflcopterzomg2'); ?> Code (markup): Before you can add this into the DB you first need ot connec to it, if this hasn't been done yet then place this code at the top of your file: <?php error_reporting(0); $username = "xxxxx"; $password = "xxxxx"; //No underscore allowed though $host = "localhost"; $database = "databasehere"; mysql_connect($host,$username,$password) or die("Error connecting to Database!<br>" . mysql_error()); mysql_select_db($database) or die("Cannot select database!<br>" . mysql_error()); ?> Code (markup): Hope this helped you. Greetz
No, no, NO!! There is an intrinsic function for this. $explodingcatintree = mysql_query("INSERT INTO `tablehere` (my_time) VALUES (now()); This saves a date/time stamp type value (stores date AND time in one field, rounded to nearest minute). Use standard PHP to print it any way you want (assuming you SELECTed the field back out of the database into "$mytime"): <?php echo date("m/d/Y", $mytime); ?> See the documentation for "date" to see how to print it in various formats (24-hr clock, AM/PM, time ony, date only, etc)
You can also try this guys.. /* mysql> SELECT NOW(); +---------------------+ | NOW() | +---------------------+ | 2005-10-11 21:49:15 | +---------------------+ 1 row in set (0.00 sec) */ SELECT NOW(); Hope this help you guys...