我试图验证该行是否有到达日期,那么我的函数应该进行计算,否则什么也不做。 如您在图片中看到的,第一行具有所有值,并且LOS时间正确,但是其他行没有值,并且LOS应该为空。
这是我的型号代码:
function time_ago($ptime) {
$query = null; //emptying in case
$shift_id = $_POST['shift_id'];
$arrival_time = $_POST['arrival_time'];
$query = $this->db->get_where('shift', array('shift_id' => $shift_id));
$count = $query->num_rows(); //counting result from query
if ($count = 0) {
$today = time();
$createdday = strtotime($ptime); //mysql timestamp of when post was created
$datediff = abs($today - $createdday);
$difftext = "";
$years = floor($datediff / (365 * 60 * 60 * 24));
$months = floor(($datediff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($datediff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
$hours = floor($datediff / 3600);
$minutes = floor($datediff / 60);
$seconds = floor($datediff);
//year checker
if ($difftext == "") {
if ($years > 1)
$difftext = $years . " years";
elseif ($years == 1)
$difftext = $years . " year";
}
//month checker
if ($difftext == "") {
if ($months > 1)
$difftext = $months . " months";
elseif ($months == 1)
$difftext = $months . " month";
}
//month checker
if ($difftext == "") {
if ($days > 1)
$difftext = $days . " days";
elseif ($days == 1)
$difftext = $days . " day";
}
//hour checker
if ($difftext == "") {
if ($hours > 1)
$difftext = $hours . " hours";
elseif ($hours == 1)
$difftext = $hours . " hour";
}
//minutes checker
if ($difftext == "") {
if ($minutes > 1)
$difftext = $minutes . " minutes";
elseif ($minutes == 1)
$difftext = $minutes . " minute";
}
//seconds checker
if ($difftext == "") {
if ($seconds > 1)
$difftext = $seconds . " seconds";
elseif ($seconds == 1)
$difftext = $seconds . " second";
}
return $difftext;
}else{
return null;
}
}
答案 0 :(得分:0)
您可以向函数添加新条件,而不是SQL检查 您的函数将如下所示:
function time_ago($ptime) {
$today = time();
$createdday = strtotime($ptime); //mysql timestamp of when post was created
$datediff = abs($today - $createdday);
$difftext = "";
$years = floor($datediff / (365 * 60 * 60 * 24));
$months = floor(($datediff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($datediff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
$hours = floor($datediff / 3600);
$minutes = floor($datediff / 60);
$seconds = floor($datediff);
//year checker
if ($difftext == "") {
if ($years > 1)
$difftext = $years . " years";
elseif ($years == 1)
$difftext = $years . " year";
}
//month checker
if ($difftext == "") {
if ($months > 1)
$difftext = $months . " months";
elseif ($months == 1)
$difftext = $months . " month";
}
//month checker
if ($difftext == "") {
if ($days > 1)
$difftext = $days . " days";
elseif ($days == 1)
$difftext = $days . " day";
}
//hour checker
if ($difftext == "") {
if ($hours > 1)
$difftext = $hours . " hours";
elseif ($hours == 1)
$difftext = $hours . " hour";
}
//minutes checker
if ($difftext == "") {
if ($minutes > 1)
$difftext = $minutes . " minutes";
elseif ($minutes == 1)
$difftext = $minutes . " minute";
}
//seconds checker
if ($difftext == "") {
if ($seconds > 1)
$difftext = $seconds . " seconds";
elseif ($seconds == 1)
$difftext = $seconds . " second";
}
if ($difftext == "49 years") {
$difftext = "";
}
return $difftext;
}