PHP date time processing function summary

  • 2020-03-31 20:04:42
  • OfStack

PHP calculates the hours and breaks them down to zero
 
 
//The parameter $hours_min is an array, the format of the array is 1:10, and the return is 1 hour
 
function hours_sum($hours_min){ 

if (!is_array($hours_min)) return false; 

$tmp_arr = array(); 
foreach ($hours_min as $v){ 
$tmp_arr = explode(':',$v); 
$hour[] = $tmp_arr[0]; 
$min[] = $tmp_arr[1]; 
} 

$hours = array_sum($hour); 
$mins = array_sum($min); 

$mins = $mins >= 10 ? str_pad($mins, 2, 0, STR_PAD_RIGHT) : $mins; 
$hours += floor($mins/60); 
$hours += $mins%60 >= 30 ? 1 : 0; 
return $hours; 
} 

Convert the date to the week

 
//The input $data parameter is yy/mm/dd or yy-mm-dd, and the date of the week is returned
function getWeekDay($date) { 
$date = str_replace('/','-',$date); 
$dateArr = explode("-", $date); 
return date("N", mktime(0,0,0,$dateArr[1],$dateArr[2],$dateArr[0])); 
} 

PHP converts seconds into hours and minutes (formatted as ** hours ** minutes)

 
//Convert seconds (not timestamps) to ** hours ** minutes
function sec2time($sec){ 

$sec = round($sec/60); 
if ($sec >= 60){ 
$hour = floor($sec/60); 
$min = $sec%60; 
$res = $hour.'  hours  '; 
$min != 0 && $res .= $min.'  points '; 
}else{ 
$res = $sec.'  minutes '; 
} 
return $res; 
} 

Related articles: