Php 将我的时间转换为时间戳
我有一个当前时间格式化方法,它是:Php 将我的时间转换为时间戳,php,mysql,Php,Mysql,我有一个当前时间格式化方法,它是: $time = date('mdY'); 我需要把它转换成一个普通的linux时间戳。我需要马上做这件事。基本上,一个函数将$time传递给它,然后它需要将它转换为一个普通的时间戳(我假设它是time();) 多谢各位 编辑: 当我尝试每个人的明显建议时,我得到了一个 警告:第75行的/home/content/50/5975650/html/checkEntry.php被零除 function RelativeTime($timeToSend){
$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);
}
这样使用:
$tm = strptime($time, '%m%d%Y');
$timestamp = strtotime(sprintf("%04d-%02d-%02d", $tm['tm_year'], $tm['tm_mon']+1, $tm['tm_mday']));
如果使用PHP5.3或更高版本,也可以使用
至于问题的另一部分,如果时差大于10年,则代码有一个bug
除法前差值的初始值是多少?我的猜测是超过10年,所以“长度”用完了,也就是说,系统试图达到未定义的$Length[8],它计为零,所以它试图除以该零。开始,函数的第一行
$difference=time()-$timestamp
引用未设置的$timestamp,因为$timeToSend
是传递到函数中的内容。这会导致$difference始终等于time()
其次,被零除的错误来自for循环for($j=0;$difference>=$length[$j];$j++)
到达一个点,$length[j]
不存在(因为第一个错误,$difference==0),因此循环将继续,直到$j
超出$length
的界限。我建议在for循环中检查$length[$j]
,就像($j=0;数组键存在($j,$length)&&&$difference>=$length[$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));
不是我的下一票,但堆栈溢出上有太多重复的内容。另外,在谷歌中输入你的问题标题也可以找到答案:显示导致错误的代码error@Pekka我已经用代码进行了更新。这与strotime()
无关-更可能的是$length[$j]
为零。添加一个防止除法发生的检查如果是这种情况我复制并粘贴了这个,我不擅长PHP数学,你能帮我解决并提供答案吗?解析错误:语法错误,意外“;”在第二行,第二行末尾缺少右括号,仍然表示不能除以零。我修复了括号。当代码中没有除法时,我真的不明白我写的这两行代码如何报告零除法错误。显然,上面的代码中有零除法,这与问题的标题完全不同。@Milanbauškov,整个脚本都在问题中发布。没别的了。嗨,乔纳森,写得很好!!我仍然收到一个警告:在第75行的/home/content/50/5975650/html/checkEntry.php中被零除的行是:$difference/=$length[$j]@jdadi尝试向for循环添加大括号{and}
,然后将$j
与$length[$j]
一起回显,以确定$j是否/在何处超出了$length的界限。