【发布时间】: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数学不好,你能帮我解答一下吗?