473,799 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

arithmetics with timestamp?

Hi everubody,

I have a timestamp field in a mysql DB and I want to know how much time
has past since that timestamp.

is it possible?
Say that I have this value in a variable -
1) how do I get the current system timestamp?
2) how do I subtract the first from the second?

Thanks!

Jan 15 '06 #1
8 1832
Super Mango wrote:
Say that I have this value in a variable -
1) how do I get the current system timestamp?
2) how do I subtract the first from the second?


If you are only interested in the amount of hours, you could do something
like:

$now = time();
$past = time() - 86400;

print 'The difference between $past and $now = ';
echo ($now - $past) / 3600, ' hours';

Of course, you can narrow this figure down to days (div 86400), hours (div
3600), minutes (div 60) and seconds.

These kind of calculations can also be performed by MySQL. Have a look at
the manual @ mysql.com for more info.
JW
Jan 15 '06 #2
thanks!

But the thing is that the value in the DB is kept as YYYY-MM-DD hh:mm
how do I convert that to the "time()" format?

Jan 15 '06 #3
Super Mango wrote:
Hi everubody,

I have a timestamp field in a mysql DB and I want to know how much time
has past since that timestamp.

is it possible?
Say that I have this value in a variable -
1) how do I get the current system timestamp?
2) how do I subtract the first from the second?

Thanks!

I can show you how to do it at a website down the wire using a bash
script. Should be adaptable to something local and PHP.

TIMESTAMP=$(dat e -d "`lynx -error_file=lynx _error -dump
-connect_timeout =20 $url/timestamp.txt`" +%s)
Jan 15 '06 #4
Super Mango wrote:
thanks!

But the thing is that the value in the DB is kept as YYYY-MM-DD hh:mm
how do I convert that to the "time()" format?


Per example, with strtotime: http://www.php.net/strtotime

JW
Jan 15 '06 #5
>I have a timestamp field in a mysql DB and I want to know how much time
has past since that timestamp.

is it possible?
Yes. Use MySQL to do it.
Say that I have this value in a variable -
1) how do I get the current system timestamp?
now()
2) how do I subtract the first from the second?


timestampdiff() . (requires MySQL 5.0 or greater).
timediff(), but it returns the value in hours, minutes, and seconds.
datediff(), but only the date parts are used.

Gordon L. Burditt
Jan 15 '06 #6
Super Mango wrote:
But the thing is that the value in the DB is kept as YYYY-MM-DD hh:mm
how do I convert that to the "time()" format?


select UNIX_TIMESTAMP( column) from table;

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jan 15 '06 #7
Super Mango wrote:
I have a timestamp field in a mysql DB and I want to know how much time
has past since that timestamp.


<?php
// Assume timestamp from database is in $ts.
$then = strtotime($ts);
$now = time();
$diff = $now - $then;
// $diff is the difference in seconds.
?>

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 16 '06 #8
d
"Super Mango" <li********@gma il.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hi everubody,

I have a timestamp field in a mysql DB and I want to know how much time
has past since that timestamp.

is it possible?
Say that I have this value in a variable -
1) how do I get the current system timestamp?
2) how do I subtract the first from the second?

Thanks!


If you're going to be doing the vast majority of your time maths outside
MySQL, you might want to use integers instead of timestamps - you can just
save the unix timestamp in the database, which saves time converting between
MySQL and unix times. And, of course, you can still search/sort using the
unix timestamp...
Jan 16 '06 #9

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

Similar topics

13
9305
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and three dropdow boxes for hours, minutes, and AM/PM. All of these need to be considered together and converted to one Unix Timestamp and then inserted to the MYSQL date field. The type of field is INT (11) so that I can instead of the standard...
6
1220
by: Terence | last post by:
I need some clarification with pointer arithmetics on void *. Example 1: ======== char s; char *ptr = s; ptr += 1; // I assume ptr is increased by 1 byte, pointing to the 2nd element in the array, right? Example 2:
21
2037
by: Milan Èermák | last post by:
Hi all, I'm about to write an arithmetics object library. It should contain classes like ShortInteger (for 32-bit longs), Double and Float as standard numeric type wrappers and LongInteger (really long numbers, not only 64-bit) and Fraction (to express high precission). All these classes may inherit from abstract class Number. I got stuck on following point. I'm not sure how to implement basic arithmetic operations. What the result type...
1
366
by: Jonas Ernst | last post by:
Hi, Can somebody give me some hints how to do a line interpolation without using floating point arithemtics? The function shall do a linear interpolation between 2 points (line interp?) and it shall return the y value to any x value given. e.g. p1: (0,0) p2 (1000,50) y value to calculate at x=600
1
1565
by: Rein Anders Apeland | last post by:
Hi all, We are developing a PoS application for Linux and are in need of a LGPL'ed og other 'free'-licensed library that can do exact decimal arithmetics for us. It should be similar or according to the specs at http://www2.hursley.ibm.com/decimal/ Somebody pointed my to the GNU Multiple Precision library, but that still
2
5185
by: rong.guo | last post by:
Hello Group, I am having a really weird problem... Can anyone tell the difference between Query 1 and Query 2 below? Why Query 2 excludes '2/28/2005'? Many thanks! create table a (ApplicationID int ,timestamp datetime)
2
4374
by: Russell Smith | last post by:
Timestamps support infinity. However if appears dates do not. When timestamps are cast to dates, there is no output. Is this an acceptable option or not? Below are a number of examples showing what I am experiencing. The last own shows how converting timestamps to dates and then ordering doesn't give you the order you want. Maybe you should just order by the timestamp to begin with. However Date does not understand infinity at all.
22
6433
by: Mal Ball | last post by:
I hope I have the right forum for this question. I have an existing Windows application which uses a SQL Server database and stored procedures. I am now developing a web application to use the same database. The original Update and Delete SP's all use a timestamp for concurreny checking. I am trying to use the same Update SP from my sqlDataSource but I keep getting the following error:
7
6117
by: JJ | last post by:
How do I set one field to have the updated timestamp, and another to have the created timestamp? I want to do this directly from code generated from DB Designer if possible?! JJ
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9077
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.