问题描述
我正在创建一个表单来设置发布间隔,并希望将所有提交的值求和成秒.该表单包含确定秒,分钟,小时,天等的字段.
I am creating a form to set publish interval and want to sum all of the submitted values into seconds. The form consist of fields to determine seconds, minutes, hours, day and so on.
我正在通过提供日期属性(例如秒,分钟等)来将提交的值收集到字符串中.
I am collecting the submitted values to string by giving date attributes, like second, minute etc.
例如:
$ second秒
$ minute分钟
$minute minute
$ hour hour
$hour hour
因此,如果用户在输入字段中输入1来确定day
中的间隔,并在minute
中确定30,则我的代码会将其转换为字符串1 day
和30 minute
.
So if user input 1 in input field to determine the interval in day
and 30 in minute
, then my code will convert it to string 1 day
and 30 minute
.
我的问题:
1-如何将1 day
和30 minute
字符串转换为秒,以便可以将它们总计为正确的秒.
1- How can I convert 1 day
and 30 minute
string into seconds so they can be summed up into correct seconds.
我尝试使用strtotime()
,但是它返回的秒数非常大,而date()
却返回的秒数太小.
I tried to use strtotime()
but it returns a very large number of seconds and date()
but it return too small number.
2-是否有像strtotime()
和date()
这样的简单函数可以正确进行字符串到秒的转换?
2- Is there a simple function like strtotime()
and date()
that can do string to second conversion correctly?
推荐答案
您可以将strtotime()
与第二个参数一起使用,如下所示:
You can use strtotime()
with a second parameter, like following:
strtotime('1 day 30 second', 0);
这将为您提供86430
,这是1天30秒的秒数.
This will give you 86430
, which is the number of seconds in 1 day and 30 seconds.
(如果省略第二个参数,则使用当前时间戳(即time()
)添加字符串中指定的内容,从而导致数字较大.)
(If the second parameter is omitted, the current timestamp (i.e. time()
) is used to add what is specified in the string, which leads to the big number.)
这篇关于php-如何将1天转换为秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!