Jump to content

Storing time duration


NotionCommotion

Recommended Posts

I have an input field for time value and a select menu for the units (hours, days, weeks, months, or years)

 

The application needs seconds (average is okay as not every month is equal, etc).

 

I need to later be able to display the time duration that was entered (i.e. 18 months).

 

Am I stuck storing the original value and the units?

Link to comment
Share on other sites

I don't care about start and finish, but only duration.

 

How long is the TV show you watched yesterday, requinx?  Ah, one hour!  How long would it be if you watched it today?

 

I actually do care about start and finish, but start is always exactly right now.

 

Wow...deep...completely unhelpful, but deep none the less.

 

Can you explain your current system in a bit more detail? Also, what exactly are you looking to change? Are you in complete control of the back end - including database design? Are you even using a database?  We need more information to provide effective suggestions at a solution.

Link to comment
Share on other sites

Wow...deep...completely unhelpful, but deep none the less.

 

Can you explain your current system in a bit more detail? Also, what exactly are you looking to change? Are you in complete control of the back end - including database design? Are you even using a database?  We need more information to provide effective suggestions at a solution.

 

 

Well, I don't know how deep, but thanks anyway.

 

I am messing around with a database called influx which requires RFC3339 UTC time stamps in the WHERE clause.

 

The user selects a duration such as 1 hour, 5 days, 3 years, etc.

 

I then wish to calculate some aggregate value based on right now going back the given duration.

 

I wish to store that duration in a SQL database so that I may do the query later.

 

Thanks

Link to comment
Share on other sites

Click the info button on your remote control and it will give you the start and end time of your show. You could also look in the TV Guide if you have one. Some systems can give you all the start and end times for that particular show during the programming period.

 

* split posted for some reason

Edited by benanamen
Link to comment
Share on other sites

Click the info button on your remote control and it will give you the start and end time of your show. You could also look in the TV Guide if you have one. Some systems can give you all the start and end times for that particular show during the programming period.

 

This is actually helpful!

 

Maybe I don't need to later be able to display the time duration that was entered (i.e. 18 months).  But instead, display the duration differently (1 year 8 months).

 

But then again, if the user entered 58 weeks, this would be displayed as 1 year, 1 month and 2 weeks.  I think 58 weeks is more intuitive.

Link to comment
Share on other sites

Well if you are always taking now as a reference then you can use a php dateTime calculation to work out the duration to the millisecond based on now minus your duration before converting and storing it. However if you are required to store and work with data accurately down to the second you should really be capturing that as part of your input - it's the only way you can get that accuracy.

Link to comment
Share on other sites

Well if you are always taking now as a reference then you can use a php dateTime calculation to work out the duration to the millisecond based on now minus your duration before converting and storing it. However if you are required to store and work with data accurately down to the second you should really be capturing that as part of your input - it's the only way you can get that accuracy.

 

I could calculate the date using the following:

$data=date(DATE_RFC3339,time()-$duration_in_seconds);

I can calculate the seconds using on of the following:

$seconds_per_hour=60*60;
$seconds_per_day=24*$seconds_per_hour;
$seconds_per_week=7*$seconds_per_day;
$seconds_per_year=365.256366*$seconds_per_day;
$seconds_per_month=$seconds_per_year/12;

I just wish to store either the quantity of hours, days, weeks, months, or years based on how the user entered them.

 

I will just store both the value in seconds, and in a second column, store the units so that I may be able to convert it back to the original format.

Edited by NotionCommotion
Link to comment
Share on other sites

The user should not dictate how you store the data. Allow the user whatever format you decide for him to enter it but piece it back together to store it as a proper date time field. You can parse that out anyway you want later.

 

Agree the user should not dictate how data is stored.

 

But how is a duration (not an instant in time) stored as a date time field?

Link to comment
Share on other sites

Since I don't know what it is you're actually trying to accomplish I can't give you a good answer. You have provided no code. Thats pretty much the first thing you need to provide if you have it. How about an explanation of what you're trying to accomplish, the big picture. (Not how to do what you think needs to be done.)

 

The ol what have you tried, what was the result, what is the expected result, along with a good overview of what is to be accomplished... I am sure you know of this.

Edited by benanamen
Link to comment
Share on other sites

Generally I just use an INT field and store the number of seconds. I'll use a function to convert that to a readable format for display, which could be exact or vague depending on what your requirements are.

 

There is also the TIME data type which could be used, but I find INT to be more flexible.

Link to comment
Share on other sites

Generally I just use an INT field and store the number of seconds. I'll use a function to convert that to a readable format for display, which could be exact or vague depending on what your requirements are.

 

There is also the TIME data type which could be used, but I find INT to be more flexible.

 

Thanks kicken,  Yea, that is exactly what I am going to do.  I've never used the TIME datatype before, but upon reading about it, agree it would work as it doesn't specify an instance in time (unlike DATETIME), but also agree it doesn't seem as flexible as a plan old INT.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.