tags:

views:

73

answers:

2

I have a bug reported whereby the user selects a date frame from a dropdown, hours, days, months and enter a freetext number.

When it's saved this is converted to a number of seconds.

I need to display this field as currently it's not being displayed. How can I work out how to make it into the same value they entered? We do not store the value of the dropdown for some strange reason.

Is there a way to just convert it to the maximum number of time block available for that number of seconds, or shall I just store the dropdown field, which is what I'm likely going to do.

+2  A: 

Converting is not an option, since you don't know anything about what the user meant. For example, when I input the number '3', you can't determine if I meant 3 days or 3 months.

Let me elaborate a bit: if I input '3' and select 'days', the time in seconds is: 60 * 60 * 24 * 3 = 259200 seconds. When displaying, you could divide it so that the output is '3 days' again. But what if I inputted '72' and selected 'hours'? You can't tell.

Just store the users choice and you're fine.

Evert
I don't think that's correct. The user _chooses_ `days` or `month` and thus something like either 259200 or 7776000 is stored in the database.
VolkerK
Hey VolkerK: I've added a bit of explanation, see the second paragraph.
Evert
I agree, I shall store the time period. It's just odd this wasnt stored already.
DavidYell
A: 

If you are looking to split it into chunks dependent such as days/months fairly simple maths operations inside a block should do the trick.

if($seconds > 2629743){
    // code for month 
}
elseif ($seconds > 86400) {
        // code for days
    }
piddl0r
@piddlor: that does not make it 'into the same value they entered'.
Evert
just providing an alternate answer as the last part of the question asks "Is there a way to just convert it to the maximum number of time block available for that number of seconds"
piddl0r