我正在尝试转换我已经实现的日期格式:
$dateEntered = date('mdY');
使用此功能的相对时间:
function RelativeTime($dateEntered)
现在,运行所有这些的实际脚本如下所示:
<?php
function RelativeTime($dateEntered){
$difference = time() - $dateEntered;
$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; array_key_exists($j,$lengths)&&$difference >= $lengths[$j]; $j++) {
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] $ending";
return $text;
}
}
// Check Service Call Status & Mail if found Unpaid
$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];
$timeToSend = RelativeTime(strtotime($dateEntered));
// mailStatusUpdate($account, $status, $timeToSend);
}
?>
如果我运行脚本,我会得到“4年前”的回报。
我的服务表中有一条记录。 $ dateEntered变量返回:
01302012
这显示在下面的print_r
输出中。我基本上想要这个说“x天前”或无论增量是什么。
它不返回正确的信息(例如x天前),而是返回:
4 decades ago
显然这是错误的。这是一个打印行,因此您可以看到我正在使用的内容:
Array ( [0] => 26 [1] => Example Client [2] => Unpaid [3] => 01302012 ) 1
(我不知道最后的“1
”是什么。)
答案 0 :(得分:0)
您是否确认strtotime()
使用情况与原始timestamp
值相符$dateEntered
?
特别是在这一行:$timeToSend = RelativeTime(strtotime($dateEntered));
修改强>
您也可以尝试将原始日期格式转换为strtotime()
的YYYY-MM-DD,如下所示:
$dateEntered = substr($dateEntered, 4,4) . '-' . substr($dateEntered, 0, 2) . '-' . substr($dateEntered, 2,2);
这采用原始格式:01302012
并将其置于以下格式:2012-01-30
。然后,您可以像这样使用strtotime()
:
strtotime($dateEntered);
答案 1 :(得分:0)
echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('2013-05-01 00:22:35', true);
4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
<强> Link to the function. 强>
如果您希望在数十年内增加支持,可以像数周一样实施。