NotionCommotion Posted June 16, 2016 Share Posted June 16, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/ Share on other sites More sharing options...
Muddy_Funster Posted June 16, 2016 Share Posted June 16, 2016 Well that depends. Could you please give a more detailed description of what the problem actually is? Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533733 Share on other sites More sharing options...
benanamen Posted June 16, 2016 Share Posted June 16, 2016 Any duration needs a start and end. Simply have a start and end datetime columns in your database, unless one of them is calculated off one of the dateimes, then you just need one date time column that is either a start or end date time. Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533734 Share on other sites More sharing options...
requinix Posted June 16, 2016 Share Posted June 16, 2016 Any duration needs a start and end.I'm watching a TV show with a duration of 1 hour. When does it start and end? Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533735 Share on other sites More sharing options...
Muddy_Funster Posted June 16, 2016 Share Posted June 16, 2016 From the little I can determine from the OP they are storing the duration as a direct input. @requinix - that was cruel, and made me giggle. Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533736 Share on other sites More sharing options...
NotionCommotion Posted June 16, 2016 Author Share Posted June 16, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533737 Share on other sites More sharing options...
Muddy_Funster Posted June 16, 2016 Share Posted June 16, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533738 Share on other sites More sharing options...
NotionCommotion Posted June 16, 2016 Author Share Posted June 16, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533740 Share on other sites More sharing options...
benanamen Posted June 16, 2016 Share Posted June 16, 2016 I'm watching a TV show with a duration of 1 hour. When does it start and end? Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533742 Share on other sites More sharing options...
benanamen Posted June 16, 2016 Share Posted June 16, 2016 (edited) 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 June 16, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533743 Share on other sites More sharing options...
NotionCommotion Posted June 16, 2016 Author Share Posted June 16, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533744 Share on other sites More sharing options...
Muddy_Funster Posted June 16, 2016 Share Posted June 16, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533745 Share on other sites More sharing options...
NotionCommotion Posted June 16, 2016 Author Share Posted June 16, 2016 (edited) 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 June 16, 2016 by NotionCommotion Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533747 Share on other sites More sharing options...
benanamen Posted June 16, 2016 Share Posted June 16, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533750 Share on other sites More sharing options...
NotionCommotion Posted June 17, 2016 Author Share Posted June 17, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533757 Share on other sites More sharing options...
benanamen Posted June 17, 2016 Share Posted June 17, 2016 (edited) 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 June 17, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533758 Share on other sites More sharing options...
kicken Posted June 17, 2016 Share Posted June 17, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533760 Share on other sites More sharing options...
NotionCommotion Posted June 17, 2016 Author Share Posted June 17, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301350-storing-time-duration/#findComment-1533763 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.