转换"月"等词。到了约会时间

时间:2015-05-07 10:11:21

标签: php

我正在创建一个程序,如果我输入的内容如下,"分钟","小时","天","周","月",我希望他们转换成时间戳。

就像我要输入" 8个月"格式为date('d-m-Y H:i:s');,我希望它输出00-08-0000 00:00:00。其余部分也一样。

我在http://php.net/上阅读了strtotime,但是当您插入时间格式而不是单词时,我会这样做。

这有可能吗?

我已经考虑过使用booleans,但是后来我得到了一个很大的混乱:

<?php
    <input type="text" name="time" />

    $time = ($_POST['time']);

    if (strpos($time,'hours') !== false){ int hour = 1; }
    else if (strpos($time,'hours') !== false){ int hours = 1; }
    else if (strpos($time,'day') !== false){ int day = 1; }
    else if (strpos($time,'days') !== false){ int days = 1; }
    else if (strpos($time,'week') !== false){ int week = 1; }
    else if (strpos($time,'weeks') !== false){ int weeks = 1; }
    else if (strpos($time,'month') !== false){ int month = 1; }
    else if (strpos($time,'months') !== false){ int months = 1; }
?>

它必须比这更有效率。

1 个答案:

答案 0 :(得分:1)

你可以使用,

strtotime(time,now);
//time => Required. Specifies a date/time string
//now  => Optional. Specifies the timestamp used as a base for the calculation of relative dates

这将解析任何英文文本日期时间描述到Unix时间戳。这里有一些例子:

strtotime("now");
strtotime("10 September 2000");
strtotime("+1 day");
strtotime("+1 week");
strtotime("+1 week 2 days 4 hours 2 seconds");
strtotime("next Thursday");
strtotime("last Monday");