note 71128 deleted from function.time by bobby
| From: | bobby@php.net | Date: | Mon, 13 Nov 2006 16:06:44 +0000 |
| Subject: | note 71128 deleted from function.time by bobby | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-119686@lists.php.net to get a copy of this message | ||
Note Submitter: Amir
Reason: bad code
----
Say you want to log start time and end time, and you want to
find how many hours it's between them.
Step 1.
<?PHP
$t = time ();
$b = date("Y-m-d H:i:s",$t);
print $b; //Shows something like this 2006-11-13 14:34:24
?>
Step 2.
Create a database and a table.
Say you have 2 fields with start time and end time.
CREATE TABLE times (
starttime varchar(20) DEFAULT '0000-00-00 00:00:00',
endtime varchar(20) DEFAULT '0000-00-00 00:00:00'
);
Insert starttime and endtime into your database table.
Example
starttime: 2006-11-13 14:34:24
endtime: 2006-11-13 16:34:24
Step 3.
<?PHP
//Connecting to mysql
mysql_connect(host,user,password) or die("Unable to connect to database");
@mysql_select_db("yourdatabase") or die("Unable to select database
yourdatabase");
$results = mysql_query("SELECT UNIX_TIMESTAMP(endtime)-UNIX_TIMESTAMP(starttime)
FROM times");
$s = mysql_result($results,0);
print ($s /60) /60; //Will print 2 hours, as result of our examples.
?>