【问题标题】:how to validate unix time with RegExp on PHP? [duplicate]如何在 PHP 中使用 RegEx 验证 unix 时间? [复制]
【发布时间】:2012-05-30 05:49:41
【问题描述】:

可能重复:
Check whether the string is a unix timestamp

我需要在 php 上使用正则表达式验证 unix 时间。 示例:

<?php
is_unix( $unix )
{
    if( preg_match( '/([0-9]+)/' , $unix ) )
    {
        return true;
    }
    return false;
}
?>

谢谢 ;)

【问题讨论】:

  • 任何介于零和time() 产生的数字都是有效的
  • 您的 RegEx 仅检查变量中是否至少出现一个数字。要检查整个值,您可以使用/^\d+$/(以确保它从字符串的开头检查到结尾)或将值转换为 int 并检查两者是否相等:(int)$unix == $unix

标签: php regex validation preg-match


【解决方案1】:

怎么样:

function is_unix($t) {
    if ( is_numeric($t) && 0<$t && $t<strtotime("some date in the future that you don't expect the value to exceed, but before year 2038") ) {
        return true;
    }else{
        return false;
}

【讨论】:

  • the year 2038替换some date in the future that you don't expect the value to exceed?
  • @Mark:谢谢,我加了一点。
猜你喜欢
  • 2011-03-02
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
  • 2013-05-13
  • 2016-03-23
  • 2013-06-16
相关资源
最近更新 更多