【问题标题】:Turn my time into a timestamp php把我的时间变成时间戳php
【发布时间】:2012-02-02 16:52:43
【问题描述】:

我有一个当前时间格式化方法是:

$time = date('mdY');

我需要把它转回正常的 linux 时间戳。我需要即时执行此操作。基本上,一个函数将 $time 传递给它,然后它需要将其转换为正常的时间戳(我假设是 time();)?

谢谢

编辑: 当我尝试每个人明显的建议时,我得到一个 警告:第 75 行 /home/content/50/5975650/html/checkEntry.php 中除以零

function RelativeTime($timeToSend){
    $difference = time() - $timestamp;
    $periods = array("sec", "min", "hour", "day", "week", "month", "years", "decade");
    $lengths = array("60","60","24","7","4.35","12","10");

    if ($difference > 0) { // this was in the past
        $ending = "ago";
    } else { // this was in the future
        $difference = -$difference;
        $ending = "to go";
    }
    for($j = 0; $difference >= $lengths[$j]; $j++)
        $difference /= $lengths[$j];
    $difference = round($difference);
    if($difference != 1) $periods[$j].= "s";
    $text = "$difference $periods[$j] $ending";
    return $text;
}

// 如果发现未付费,请检查服务呼叫状态和邮件

$query = "SELECT id, account, status, dateEntered FROM service WHERE status = 'Unpaid'";
        $result = mysql_query($query) or die(mysql_error());

    while($row = mysql_fetch_row($result)){

                $account = $row[1];
                $status = $row[2];
                $dateEntered = $row[3];

                $timestamp = strtotime($dateEntered);   
                            $timeToSend = RelativeTime($timestmap);

              // mailStatusUpdate($account, $status, $timeToSend);

        }   

【问题讨论】:

  • 不是我的反对意见,但在 Stack Overflow 上有 所以 很多重复。另外,在 Google 中输入您的问题标题也可以找到答案:php.net/strtotime
  • 显示导致错误的代码
  • @Pekka 我已经更新了代码。
  • 这与 strtotime() 无关 - $lengths[$j] 更可能为零。如果是这种情况,请添加一个检查以防止发生除法
  • 我复制粘贴了这个,我对PHP数学不好,你能帮我解答一下吗?

标签: php mysql


【解决方案1】:

开始,函数$difference = time() - $timestamp; 的第一行引用$timestamp 未设置,因为$timeToSend 是传递给函数的内容。这会导致 $difference 始终等于 time()

其次,除以零错误是从 for 循环 for($j = 0; $difference >= $lengths[$j]; $j++) 到达 $lengths[j] 不存在的点(因为第一个错误,$difference==0),所以循环继续直到 @ 987654327@ 超出了$lengths 的范围。我建议在 for 循环中检查 $lengths[$j],例如 for($j = 0; array_key_exists($j,$lengths)&&$difference >= $lengths[$j]; $j++)

最终:

function RelativeTime($timeToSend){
$difference = time() - $timestamp;
$periods = array("sec", "min", "hour", "day", "week",
"month", "years", "decade");
$lengths = array("60","60","24","7","4.35","12","10");

if ($difference > 0) { // this was in the past
$ending = "ago";
} else { // this was in the future
$difference = -$difference;
$ending = "to go";
}
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] $ending";
return $text;
}
echo RelativeTime(strtotime('-3 months'))."\n";
echo RelativeTime(strtotime(0));

And example working.

【讨论】:

  • 嗨乔纳森,写得很好!我仍然收到警告:在第 75 行的 /home/content/50/5975650/html/checkEntry.php 中除以零 该行是:$difference /= $lengths[$j];
  • @JDAudi 尝试在 for 循环中添加大括号 { and },然后回显 $j$lengths[$j] 以找出 $j 是否/在哪里超出了 $lengths 的范围。
【解决方案2】:

像这样使用strptime

$tm = strptime($time, '%m%d%Y');
$timestamp = strtotime(sprintf("%04d-%02d-%02d", $tm['tm_year'], $tm['tm_mon']+1, $tm['tm_mday']));

如果你使用PHP 5.3或以上,也可以使用date_parse_from_format

至于您问题的另一部分,如果时差大于 10 年,您的代码就有错误。

除法前的初始差值是多少?我的猜测是超过 10 年,所以你用完了“长度”,即系统试图达到未定义的 $lengths[8],它算作零,所以它试图除以那个零。

【讨论】:

  • 解析错误:语法错误,意外';'在你的第二行
  • 第二行末尾缺少右括号
  • 还是说不能除以零。
  • 我固定了支架。当代码中没有除法时,我真的看不出我写的那两行如何报告除以零错误。除以零显然在您的代码中,这与您的问题标题完全不同。
  • @MilanBabuškov ,整个脚本发布在问题中。没有别的了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
  • 2019-04-05
  • 2012-07-24
相关资源
最近更新 更多