开发者

Converting seconds into period of time (PHP)

开发者 https://www.devze.com 2023-01-14 22:59 出处:网络
I have a bug reported whereby the user selects a date frame from a dropdown, hours, days, months and enter a freetext number.

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 num开发者_如何学运维ber 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.


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.


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
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号