473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Need help with PHP & SQL code, new to this.

JA
I am trying to pull a time stamp out of a database,looks like this
2004-06-24 10:12:03 . Then I need to substring it so that I can take
a difference of 2 times. Basically I making a clock in/clock out
script for my job. But I can't figure out the query and then how to
substring the time so that I can do math with just the time part and
not the date. Or if you guys/gals no if any code out there, were
someone has written a script like this, that would be great. I am
willing to send my code if you need it. It should be easy to code,
but i've never done PHP before. Thanks in advance.

Jimmy Allen
Jul 17 '05 #1
3 1839
On 24 Jun 2004 09:59:05 -0700, ji*********@hotmail.com (JA) wrote:
I am trying to pull a time stamp out of a database,looks like this
2004-06-24 10:12:03 . Then I need to substring it so that I can take
a difference of 2 times.

You can find the difference between two timestamps with a select from
the database.
Something like elect t_start, t_end, time_format((t_start - t_end),
'%H:%M%S) from testtab;

But I can't recall how to convert the hours (1 hr = 100, not 60)

You can also select each part of the timestamp into its own slot:
mysql> select extract(year from t_start) ys, extract(year from t_end)
ye, extract(month from t_start) ms from testtab;

Long but it works.
+------+------+------+------+------+------+
| ys | ye | ms | me | ds | de |
+------+------+------+------+------|------|
| 2004 | 2004 | 1 | 2004 | 2004 | 1 |
+------+------+------+------+------+------+
--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://www.databasix.com
================================================== =========================
Jul 17 '05 #2
JA wrote:
I am trying to pull a time stamp out of a database,looks like this
2004-06-24 10:12:03 . Then I need to substring it so that I can take
a difference of 2 times. Basically I making a clock in/clock out
script for my job. But I can't figure out the query and then how to
substring the time so that I can do math with just the time part and
not the date.


Hi Jimmy,

Why do you need to ignore the date - you could confuse the issue if you
have people working over the midnight hour. Why not just do the math on
the whole date/time string.

Extract the data from the database, using something like:
SELECT login_time from tbl_whatever;
SELECT NOW();

- assigning each to a variable within your script. I've hardcoded the
date/time string below for ease, but you would populate them using the
queres above. Using NOW() will give you the current time from the MySQL
server.

run your two dates through a function which does this:
<?php
$time1 = strtotime("2004-06-01 07:30");
$time2 = strtotime("2004-06-01 18:32");

$interval = $time2 - $time1;

echo "The time difference is : {$interval} seconds<br>";
echo "which equates to : ".strftime("%H:%M", $interval);

?>

Hope this helps, regards,

Andy

Jul 17 '05 #3
JA wrote:
I am trying to pull a time stamp out of a database,looks like this
2004-06-24 10:12:03 . Then I need to substring it so that I can take
a difference of 2 times. Basically I making a clock in/clock out
script for my job. But I can't figure out the query and then how to
substring the time so that I can do math with just the time part and
not the date. Or if you guys/gals no if any code out there, were
someone has written a script like this, that would be great. I am
willing to send my code if you need it. It should be easy to code,
but i've never done PHP before. Thanks in advance.

Jimmy Allen


From the MySQL Docs:

TIMEDIFF(expr,expr2)
TIMEDIFF() returns the time between the start time expr and the end
time expr2. expr and expr2 are time or date-and-time expressions, but
both must be of the same type.

mysql> SELECT TIMEDIFF('2000:01:01 00:00:00',
-> '2000:01:01 00:00:00.000001');
-> '-00:00:00.000001'
mysql> SELECT TIMEDIFF('1997-12-31 23:59:59.000001',
-> '1997-12-30 01:01:01.000002');
-> '46:58:57.999999'

TIMEDIFF() was added in MySQL 4.1.1.
Michael Austin.
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: |-|erc | last post by:
<?php // Get the names and values for vars sent by index.lib.php3 if (isset($HTTP_GET_VARS)) { while(list($name,$value) = each($HTTP_GET_VARS)) { $$name = $value; }; };
4
by: Gerry | last post by:
As I'm not a PHP-prgrammer at all, I just need Help with this: I have had a guestbook-page in Europe and will now have to move it to a US based-server. This makes the time-function showing time...
7
by: Paul Charlton-Thomson | last post by:
Hi! I have nearly cracked this and am stuck on the last bit. I am asking my visitors to rank 8 different items (a,b,c,d,e,f,g,h) from 1 to 10 and then I want to draw a pie chart of those values....
2
by: DC Gringo | last post by:
I have an image control (that pulls an image off an ESRI map server): <ASP:IMAGE ID="imgZonedCountry" RUNAT="server"></ASP:IMAGE> In the code behind I am setting the ImageURL to a String value...
4
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: ...
4
by: yaffa | last post by:
dear folks, i'm trying to append a semicolon to my addr string and am using the syntax below. for some reason the added on of the ; doesn't work. when i print it out later on it only shows the...
10
by: | last post by:
In .NetCF, upon trying to access the following URL: http://rss.news.yahoo.com/rss/business via the following code: WebRequest webReq = WebRequest.Create(url); WebResponse webResp =...
11
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.