php的一些实用方法记录(经纬度计算,星期计算)

本文介绍了一个实用的PHP函数,用于计算地球上任意两点之间的距离,并提供了一组日期相关的辅助函数,帮助开发者轻松处理星期和日期转换的问题。
/**
* @desc 根据两点间的经纬度计算距离
* @param float $lat 纬度值
* @param float $lng 经度值
*/
function getDistance($lat1,$lng1,$lat2,$lng2)
{
$earthRadius= 6367000;//approximate radius of earth in meters
 
/*
Convert these degrees to radians
to work with the formula
*/
 
$lat1 = ($lat1 * pi() ) / 180;
$lng1 = ($lng1 * pi() ) / 180;
 
$lat2 = ($lat2 * pi() ) / 180;
$lng2 = ($lng2 * pi() ) / 180;
 
 
$calcLongitude=$lng2 - $lng1;
$calcLatitude=$lat2 - $lat1;
$stepOne= pow(sin($calcLatitude/ 2), 2) +cos($lat1) *cos($lat2) * pow(sin($calcLongitude/ 2), 2);
$stepTwo= 2 * asin(min(1, sqrt($stepOne)));
$calculatedDistance=$earthRadius * $stepTwo;
 
return round($calculatedDistance);
}
//星期函数  返回当前是今年的第几周 ----->每周天是每周的开始 Ymd return YW
function getWeek($str = ''){
  $str = empty($str) ? date('Ymd', time()) : date('Ymd', strtotime($str));
  $m = date('Y', strtotime($str));
  for($i=1; $i <8; $i++){
    if(date('w', strtotime(substr($str,0,4).'01'.sprintf('%02d',$i))) == 0){
      $time = strtotime(substr($str,0,4).'01'.sprintf('%02d',$i));break;
    }
  }
  if(!isset($time)) return false;
  if((strtotime($str)-$time) > 0)
    return $m.sprintf('%02d', intval((strtotime($str)-$time)/60/60/24/7)+1);
  elseif((strtotime($str)-$time) == 0)
    return $m.'01';
  else{
    $day = getWeek(date('Y-m-d',strtotime($str)-(7*24*60*60)));
    if(substr($day, 4, 2) == '52'){
      return ($m-1)."53";
    }else{
      return ($m-1)."52";
    }
  }
}

//日期函数 根据星期数返回该星期的日期 周日为每周第一天 || 周一为第一天 YW return 该周开始日期
function weekToDay($str , $type = 0){
  $week = substr($str, 4, 2);
  if($week < 1 || $week > 54) return false;
  $day  = ((int)$week-1) * 7;
  for($i=1; $i <8; $i++){
    if(date('w', strtotime(substr($str,0,4).'01'.sprintf('%02d',$i))) == $type){
      $year = substr($str,0,4).'01'.sprintf('%02d',$i);break;
    }
  }
  if(!isset($year)) return false;
  return date('Y-m-d', strtotime("+$day day", strtotime($year)));
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__DEBUG__

来杯可乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值