473,387 Members | 1,742 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,387 software developers and data experts.

Dates Differences from Strings

Ike
If I have two String objects which are formatted date-times, say for
example:

'20060127 01:00:03'
and
'20060217 13:41:17'

Is there a means by which I could redisplay this as the difference between
the two, as a String, say, like, that may show the hh:mm:ss as well as the
number of days?

Thanks, Ike

Feb 17 '06 #1
5 1247
On 17 Feb 2006 in mailing.database.mysql, Ike wrote:
If I have two String objects which are formatted date-times, say for
example:

'20060127 01:00:03'
and
'20060217 13:41:17'

Is there a means by which I could redisplay this as the difference
between the two, as a String, say, like, that may show the hh:mm:ss
as well as the number of days?


According to the MySQL 4.1 and 5.0 manuals:

DATEDIFF() returns the number of days between the start date expr
and the end date expr2. expr and expr2 are date or date-and-time
expressions. Only the date parts of the values are used in the
calculation.

mysql> SELECT DATEDIFF('1997-12-31 23:59:59','1997-12-30');
-> 1
mysql> SELECT DATEDIFF('1997-11-30 23:59:59','1997-12-31');
-> -31

http://dev.mysql.com/doc/refman/5.0/...functions.html

Your best bet might be to convert them to Unix timestamps in PHP, and
do the arithmetic and formatting there.

http://www.php.net/manual/en/function.strtotime.php
http://www.php.net/manual/en/function.date.php

--
Joe Makowiec
http://makowiec.org/
Email: http://makowiec.org/contact/?Joe
Feb 17 '06 #2
Ike
Ah yes yes....actually TIMEDIFF() will do precisely what I need! THank you.
Feb 17 '06 #3
On 17 Feb 2006 in mailing.database.mysql, Ike wrote:
Ah yes yes....actually TIMEDIFF() will do precisely what I need!
THank you.


Just be careful of the date format you go in with:

mysql> select timediff('20060127 01:00:03','20060217 13:41:17');
+---------------------------------------------------+
| timediff('20060127 01:00:03','20060217 13:41:17') |
+---------------------------------------------------+
| -2172:41:14 |
+---------------------------------------------------+
1 row in set (0.00 sec)

mysql> select timediff('2006-01-27 01:00:03','2006-02-17 13:41:17');
+-------------------------------------------------------+
| timediff('2006-01-27 01:00:03','2006-02-17 13:41:17') |
+-------------------------------------------------------+
| -516:41:14 |
+-------------------------------------------------------+
1 row in set (0.00 sec)

--
Joe Makowiec
http://makowiec.org/
Email: http://makowiec.org/contact/?Joe
Feb 17 '06 #4
Ike
Right right...it needs to be specified, say, as '2006-02-17 12:13:14' for
the calculation to occur properly, yes?
Feb 17 '06 #5
On 17 Feb 2006 in mailing.database.mysql, Ike wrote:
Right right...it needs to be specified, say, as '2006-02-17
12:13:14' for the calculation to occur properly, yes?


Looks that way.

--
Joe Makowiec
http://makowiec.org/
Email: http://makowiec.org/contact/?Joe
Feb 17 '06 #6

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

Similar topics

11
by: Amy G | last post by:
I have seen something about this beofore on this forum, but my google search didn't come up with the answer I am looking for. I have a list of tuples. Each tuple is in the following format: ...
6
by: Michael Hill | last post by:
I have this large array with dates in it. There is a function that sorts it for me. The only problem is that in order for it to get sorted properly I have to sort it AlphaNumerically first using ...
26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
11
by: Peter Pfeiffer | last post by:
I've written several scripts that have "while" blocks which increment a date by one day if the date does not match one of a group of dates. However, sometimes it apparently steps out out the while...
7
by: Bambero | last post by:
Hello all Problem like in subject. There is no problem when I want to count days between two dates. Problem is when I want to count years becouse of leap years. For ex. between 2002-11-19...
8
by: netsurfer | last post by:
Hi: Have a question on making the date automatically filled in by what the user enters in by the date at the top. The date entered at the top would most likely be on a Wednesday then I need...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
8
by: Mojca | last post by:
What is formula that get days between two dates? Npr: 11.03.1998 – 1.7.2005 Thank you, Mojca
9
by: labelle | last post by:
I have an event listing on my website that is pulling from SQL. Unfortunately, the listing is displaying all events, especially those that are even 2 years old. I want to get rid of them and only...
23
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...

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.