Snooble Posted December 8, 2008 Share Posted December 8, 2008 When I create a user in my database I want to record the date and time the record was made. Using PHPMYADMIN what type would the field be? and using php what would i put in the "INSERT" sql statement? Thanks Snooble Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/ Share on other sites More sharing options...
PravinS Posted December 8, 2008 Share Posted December 8, 2008 Use DATETIME data type for date field. You can INSERT date and time by using NOW() function in sql statement. Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709373 Share on other sites More sharing options...
redarrow Posted December 8, 2008 Share Posted December 8, 2008 Well years ago i was told to use the int for the database field and the time() php statement. database field int not null <?php $date=time(); $sql="insert into what_ever(date)values('$date')"; ?> That will create a timestamp in unix.. format a time stamp use the date function from a while loop example $date=date("h:i:s", data['date']); now the time stamp only shows the time in hours min seconds Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709374 Share on other sites More sharing options...
Mchl Posted December 8, 2008 Share Posted December 8, 2008 Use TIMESTAMP type and define default as CURRENT_TIMESTAMP Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709375 Share on other sites More sharing options...
Yesideez Posted December 8, 2008 Share Posted December 8, 2008 I use INT(10) UNSIGNED for the database entry containing a timestamp and PHP's time() to populate it. mysql_query("INSERT INTO `table` (`dt`) VALUES ('".time()."')"); When you get the date/time from the database you can display it using PHP's date() function: echo date('d-M-Y H:i:s',$row['dt']); See here for formatting the date and time: http://uk3.php.net/date The big thing I like about time() is sorting records can be done soooo easily and adding dates/times also as easy. For example, to add 1 hour use this: echo time()+(60*60); Since time() is measured in seconds 60*60 is seconds*minutes which gives 3,600 - one hour. btw, you use UNSIGNED for the MySQL database because time() is never negative and gives you the full range. Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709377 Share on other sites More sharing options...
redarrow Posted December 8, 2008 Share Posted December 8, 2008 1 year i think echo time()+(60*60*24*365); Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709382 Share on other sites More sharing options...
Mchl Posted December 8, 2008 Share Posted December 8, 2008 1 year i think echo time()+(60*60*24*365); Why not echo strtotime("+1 year") (wonder if it takes leap years into account) Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709383 Share on other sites More sharing options...
redarrow Posted December 8, 2008 Share Posted December 8, 2008 I am sure this is correct when dealing with leap years not sure theo. <?php echo date("d/m/y",time()+(60*60*24*365)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709385 Share on other sites More sharing options...
Snooble Posted December 8, 2008 Author Share Posted December 8, 2008 so i've a 10 int field. And it's saving time(). How can i output it. Format needs to be 24:00 DD/MM/YYYY Thanks Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709388 Share on other sites More sharing options...
Yesideez Posted December 8, 2008 Share Posted December 8, 2008 Check the link I gave you to format the time - if you get stuck come back and we'll prod you in the right direction. ALL the formatting codes are listed on that page. Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709393 Share on other sites More sharing options...
Mchl Posted December 8, 2008 Share Posted December 8, 2008 Why not check then? <?php echo date("d-m-Y",strtotime("+4 years")); //8-12-2008 echo "<br/>\n"; echo date("d-m-Y",time() + 4 * 365 * 24 * 3600); //7-12-2008 ?> strtotime wins again [edit] I really don't understand, why you're saving dates as INT Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709395 Share on other sites More sharing options...
Snooble Posted December 8, 2008 Author Share Posted December 8, 2008 I have looked... but it doesn't completey tell me... cause in my database i have: 1228745009 What can i do with that? can you give me a wonderful prod please!? Snoobs Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709398 Share on other sites More sharing options...
Yesideez Posted December 8, 2008 Share Posted December 8, 2008 Mchl, check my previous post as to why... Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709399 Share on other sites More sharing options...
redarrow Posted December 8, 2008 Share Posted December 8, 2008 agree strtotime wins <?php $year = date('Y'); function isleapyear($year) { $year = (int) $year; if ($year % 4 == 0) { if ($year % 100 == 0) { return ($year % 400 == 0); } else { return true; } } else { return false; } } echo isleapyear($year); ?> Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709401 Share on other sites More sharing options...
Mchl Posted December 8, 2008 Share Posted December 8, 2008 Still I see no reason to do it There are dedicated data types for time, and these should be used. Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709403 Share on other sites More sharing options...
Yesideez Posted December 8, 2008 Share Posted December 8, 2008 I find INT and time() so much more versatile than using the MySQL methods. Even the forums use the INT time() method. Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709406 Share on other sites More sharing options...
Maq Posted December 8, 2008 Share Posted December 8, 2008 I have looked... but it doesn't completey tell me... cause in my database i have: 1228745009 What can i do with that? can you give me a wonderful prod please!? Snoobs Sorry Snoobs, as these guys have steered into their own thread. The number you have is a timestamp. You have to use the date function like so... $your_date = 1228745009; echo date("d-m-Y", $your_date)); Check out the manual - date() for more date formats. Hope this is what you're looking for because I don't know what this means: can you give me a wonderful prod please!? Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709407 Share on other sites More sharing options...
redarrow Posted December 8, 2008 Share Posted December 8, 2008 now u need to format ur time how u want it. http://uk3.php.net/date <?php echo date("d-m-Y h:i:s", 1228745009); ?> [code] Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709409 Share on other sites More sharing options...
Snooble Posted December 8, 2008 Author Share Posted December 8, 2008 thanks so much maq. I'll work out the formatting! I understand now. Thanks!!!! Snoobs Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709411 Share on other sites More sharing options...
Yesideez Posted December 8, 2008 Share Posted December 8, 2008 Didn't I just post all that? Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709412 Share on other sites More sharing options...
Maq Posted December 8, 2008 Share Posted December 8, 2008 Didn't I just post all that? :P Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709416 Share on other sites More sharing options...
Mchl Posted December 8, 2008 Share Posted December 8, 2008 I find INT and time() so much more versatile than using the MySQL methods. Even the forums use the INT time() method. A matter of habit probably... Just wanted our OP to know, there are other options I can't imagine anything more convenient than TIMESTAMP DEFAULT CURRENT_TIMESTAMP Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709417 Share on other sites More sharing options...
Yesideez Posted December 8, 2008 Share Posted December 8, 2008 That's why I posted this... I use INT(10) UNSIGNED for the database entry containing a timestamp Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709420 Share on other sites More sharing options...
redarrow Posted December 8, 2008 Share Posted December 8, 2008 Got to admit Theo mysql has got some really nice time formatting Theo very powerful We have teached you the best ways to get a timestamp in your database, and shown you how to format the timestamp. Any think else relating to the post mate.... Quote Link to comment https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709421 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.