Jump to content

[SOLVED] TIMESTAMP HOW!?


Snooble

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709374
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709377
Share on other sites

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

 

[edit]

 

I really don't understand, why you're saving dates as INT :P

 

Link to comment
https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709395
Share on other sites

 

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709401
Share on other sites

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!?
Link to comment
https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709407
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/136054-solved-timestamp-how/#findComment-709417
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.