将我的时间转换为时间戳php


Turn my time into a timestamp php

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

$time = date('mdY');

我需要把它转换回一个普通的linux时间戳。我需要马上做这件事。基本上,一个函数将$time传递给它,然后它需要将其转换为一个正常的时间戳(我假设是time();)?

谢谢

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

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);
        }   

开始时,函数$difference = time() - $timestamp;的第一行引用$timestamp,因为$timeToSend是传递到函数中的内容,所以未设置该值。这导致$difference总是等于CCD_ 3。

其次,除以零的误差是从for循环for($j = 0; $difference >= $lengths[$j]; $j++)到不存在$lengths[j]的点(由于第一个误差,$difference==0),因此循环继续,直到$j超过$lengths的边界。我建议像for($j = 0; array_key_exists($j,$lengths)&&$difference >= $lengths[$j]; $j++)一样在for循环中检查$lengths[$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));

并以身作则。

像这样使用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年了,所以你用完了"长度",即系统试图达到未定义的$length[8],它算作零,所以它试图除以零。