当前位置:网站首页>PHP common date related functions
PHP common date related functions
2022-06-09 16:27:00 【Uncle Muyu】
/**
* Get the start and end time of the week of the specified date
* @param null $curTime
* @return array
*/
public function getCurWeek($curTime=null) {
$curTime = $curTime ? : date('Y-m-d');
// Gets the day ordinal of the current week Sunday is 0 Monday to Saturday is 1 - 6
$week = date('w',strtotime($curTime));
// Get the start date of the week , If $w yes 0, It means Sunday , subtract 6 God
$mon = "$curTime - ". ($week ? $week - 1 : 6)." day";
$start = date('Y-m-d',strtotime($mon));
$end = date('Y-m-d',strtotime($start.' + 6 day'));
return array('start' => $start,'end' => $end);
}/**
* Get the start and end time of the week before the specified date
* @param null $curTime
* @return array
*/
public function getPrevWeek($curTime=null) {
$curTime = $curTime ? : time();
// Gets the day ordinal of the current week Sunday is 0 Monday to Saturday is 1 - 6
$week = date('w',strtotime($curTime));
// Get the start date of the week , If $w yes 0, It means Sunday , subtract 6 God
$mon = "$curTime - ". ($week ? $week - 1 : 6)." day";
$start = date('Y-m-d',strtotime($mon.' - 7 day'));
print_r($start);
$end = date('Y-m-d',strtotime($start.' + 6 day'));
print_r($end);
return array('start' => $start,'end' => $end);
}/**
* Get the start and end time of one week after the specified date
* @param null $curTime
* @return array
*/
public function getNextWeek($curTime=null) {
$curTime = $curTime ? : time();
// Gets the day ordinal of the current week Sunday is 0 Monday to Saturday is 1 - 6
$week = date('w',strtotime($curTime));
// Get the start date of the week , If $w yes 0, It means Sunday , subtract 6 God
$mon = "$curTime - ". ($week ? $week - 1 : 6)." day";
$start = date('Y-m-d',strtotime($mon.' + 7 day'));
$end = date('Y-m-d',strtotime($start.' + 6 day'));
return array('start' => $start,'end' => $end);
}/**
* Gets the continuous time of the week of the specified date
* @param null $curTime
* @return array
*/
public function getWeeksByDate($curTime=null) {
$weeks = array(' Sunday ',' Monday ',' Tuesday ',' Wednesday ',' Thursday ',' Friday ',' Saturday ');
$weekRange = array();
$curTime = $curTime ? : date('Y-m-d');
// Gets the day ordinal of the current week Sunday is 0 Monday to Saturday is 1 - 6
$week = date('w',strtotime($curTime));
// Get the start date of the week , If $w yes 0, It means Sunday , subtract 6 God
$mon = "$curTime - ". ($week ? $week - 1 : 6)." day";
$start = date('Y-m-d',strtotime($mon));
$end = date('Y-m-d',strtotime($start.' + 6 day'));
$days = floor((strtotime($end)-strtotime($start))/86400);
for ($i=0;$i<=$days;$i++) {
$day = date('Y-m-d',strtotime($start.' '.$i.' day'));
$weekRange[] = array(
'day' => date('m-d',strtotime($day)),
'week' => $weeks[date('w',strtotime($day))],
'fullDay' => $day
);
}
return $weekRange;
}/**
* Get the start and end time of the month of the specified date
* @param null $curTime
* @return array
*/
public function getCurMonth($curTime=null){
$curTime = $curTime ? : date('Y-m-d');
$timestamp = strtotime($curTime);
$days = date('t', $timestamp);
$start = date('Y-m-01', $timestamp);
$end = date('Y-m-'.$days, $timestamp);
return array('start' => $start,'end' => $end);
}/**
* Gets the continuous time of the month of the specified date
* @param null $curTime
* @return array
*/
public function getDaysByDate($curTime=null){
$dayRange = array();
$curTime = $curTime ? : date('Y-m-d');
$timestamp = strtotime($curTime);
$days = date('t', $timestamp);
$start = date('Y-m-01', $timestamp);
for ($i=0;$i<$days;$i++) {
$day = date('Y-m-d',strtotime($start.' '.$i.' day'));
$dayRange[] = array(
'day' => date('m-d',strtotime($day)),
'fullDay' => $day
);
}
return $dayRange;
}/**
* Gets the continuous time in the specified time period
* @param $start Starting time
* @param $end By the time
* @param bool $week Whether to include weekly information , Default false
* @return array
*/
public function getDaysByRange($start,$end,$week=false) {
$dayRange = array();
$weeks = array(' Sunday ',' Monday ',' Tuesday ',' Wednesday ',' Thursday ',' Friday ',' Saturday ');
$days = floor((strtotime($end)-strtotime($start))/86400);
for ($i=0;$i<=$days;$i++) {
$day = date('Y-m-d',strtotime($start.' '.$i.' day'));
$dayRange[] = array(
'day' => date('m-d',strtotime($day)),
'fullDay' => $day
);
}
if ($week) {
foreach ($dayRange as $key => $d) {
$dayRange[$key]['week'] = $weeks[date('w',strtotime($d['fullDay']))];
}
}
return $dayRange;
}/**
* Get the start and end time of each quarter in the specified year
* @param null $year year
* @param int $quarter Quarter serial number ,0 All quarters | 1 First quarter | 2 The two quarter | 3 third quater | 4 In the fourth quarter
* @return mixed
*/
public function getQuarter($year=null,$quarter=0){
$quarters = array(
array('start' => '01-01','end' => '12-31'),
array('start' => '01-01','end' => '03-31'),
array('start' => '04-01','end' => '06-30'),
array('start' => '07-01','end' => '09-30'),
array('start' => '10-01','end' => '12-31')
);
$year = $year ? : date('Y');
$fullQuarter = $quarters[$quarter];
$fullQuarter['start'] = sprintf('%s-%s',$year,$fullQuarter['start']);
$fullQuarter['end'] = sprintf('%s-%s',$year,$fullQuarter['end']);
return $fullQuarter;
}/**
* Get the timestamp corresponding to the specified date , Default to current date
* @param string $dateTime Time , Format example :Y-m-d H:i:s
* @return int
*/
public function getTimeStamp($dateTime=''){
$date = new \DateTime($dateTime);
return $date->format('U');
}/**
* Format timestamps
* @param $time Time stamp
* @param $format Time format , Such as :Y-m-d H:i:s
* @return string
*/
public function formatTimeStamp($time,$format=null){
$format = $format ? : 'Y-m-d H:i:s';
$date = new \DateTime(sprintf('@%s',$time));
// Set China time zone
$date->setTimezone(new \DateTimeZone('PRC'));
return $date->format($format);
}/**
* Get the current timestamp , Accurate to milliseconds
* @return number
*/
public function getMicroTime(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}/**
* Format timestamps , Accurate to milliseconds ,x On behalf of ms
* @param $time Time stamp
* @param $format Time format , Such as :Y-m-d H:i:s
* @return mixed
*/
public function formatMicroTime($time,$format=null) {
if (strpos($time, '.') !== false) {
list($usec, $sec) = explode(".", $time);
}else{
$usec = substr($time,0,10);
$sec = str_replace($usec, '', $time);
}
$format = $format ? $format.'.x' : 'Y-m-d H:i:s.x';
$date = date($format,$usec);
return str_replace('x', $sec, $date);
}/**
* Get time difference
* @param $dateTime1 Format , Such as :Y-m-d H:i:s
* @param $dateTime2 Format , Such as :Y-m-d H:i:s
* @param $type Time difference type ,H Hours | m minute | s second , Default to days
* @return mixed
*/
public function getTimeDiff($dateTime1,$dateTime2,$type=null){
$diff = null;
$date1 = new \DateTime($dateTime1);
$date2 = new \DateTime($dateTime2);
if ($type) {
$seconds1 = $date1->format('U');
$seconds2 = $date2->format('U');
$temp = abs($seconds1 - $seconds2);
$type = strtoupper($type);
switch ($type) {
case 'H':
$diff = floor($temp/3600);
break;
case 'M':
$diff = floor($temp/60);
break;
case 'S':
$diff = $temp;
break;
default:
break;
}
} else {
$timeDiff = $date1->diff($date2);
$diff = $timeDiff->days;
}
return $diff;
}/**
* Time offset increment calculation
* @param $intervalArr Offset array , Format :array('year'=>'','mon'=>'','day'=>'','hour'=>'','min'=>'','sec'=>'')
* @param string $dateTime Time , Format example :Y-m-d H:i:s
* @return string
*/
public function addDateTime($intervalArr,$dateTime=''){
$date = new \DateTime($dateTime);
$interval = sprintf('P%sY%sM%sDT%sH%sM%sS',$intervalArr['year'],$intervalArr['mon'],
$intervalArr['day'],$intervalArr['hour'],$intervalArr['min'],$intervalArr['sec']);
$date->add(new \DateInterval($interval));
return $date->format('U');
}/**
* Time offset decrement calculation
* @param $intervalArr Offset array , Format :array('year'=>'','mon'=>'','day'=>'','hour'=>'','min'=>'','sec'=>'')
* @param string $dateTime Time , Format example :Y-m-d H:i:s
* @return string
*/
public function subDateTime($intervalArr,$dateTime=''){
$date = new \DateTime($dateTime);
$interval = sprintf('P%sY%sM%sDT%sH%sM%sS',$intervalArr['year'],$intervalArr['mon'],
$intervalArr['day'],$intervalArr['hour'],$intervalArr['min'],$intervalArr['sec']);
$date->sub(new \DateInterval($interval));
return $date->format('U');
}/**
* Compare date size
* @param $dateTime1 Format , Such as :Y-m-d H:i:s
* @param $dateTime2 Format , Such as :Y-m-d H:i:s
* @return int Parameters 1> Parameters 2 when , return 1, Parameters 1< Parameters 2 when , return -1, Otherwise return to 0
*/
public function compareDate($dateTime1,$dateTime2){
$date1 = new \DateTime($dateTime1);
$date2 = new \DateTime($dateTime2);
$seconds1 = $date1->format('U');
$seconds2 = $date2->format('U');
if ($seconds1 > $seconds2) {
return 1;
}
if ($seconds1 < $seconds2) {
return -1;
}
return 0;
}边栏推荐
- C#/VB.NET 在Word转PDF时生成目录书签
- 使用ffmpeg合并mp4文件
- Dapr source code analysis | basic introduction
- Read the middle office architecture and Implementation
- Introduction to database storage directory structure
- 第一章 ESQL介绍
- Laravel8 Frame Seven niuyun upload
- Laravel8框架七牛雲上傳
- [SEETF]Super Secure Requests Forwarder
- June training (day 04) - greed
猜你喜欢
![[SEETF]Super Secure Requests Forwarder](/img/b4/f3211c28a2474973e231a69d9a4ca8.png)
[SEETF]Super Secure Requests Forwarder

618's money saving technology strategy is coming - experience the scene and get a 10 yuan cat super card!

看完就会,从抓包到接口测试的全过程解析

Usage of dac8560

记一次可怜的150元RCE平安Src挖掘

Kubernetes core concepts

Build an integrated intelligent dialogue analysis platform from intelligent quality inspection to dialogue analysis, and the customer service center of Bank of Hangzhou creates a new business card of

leetcode:189.轮转数组

Customizing the in app keyboard in fluent

Differences between containers and mirrors
随机推荐
站长工具浏览器SEO插件-站长必备工具 SEO网站排名 快速查看网站数据
Deepin 编译VirtualBox实录以及编译报错解决
Selection and points for attention of desk and chair
opensuse添加开机启动项
网页加载waiting(TTFB)时间过长的解决方案参考
Reconstruction essentials learning
【第18天】给定两个变量 a 和 b,交换它们的值 | 三种解法
看完就会,从抓包到接口测试的全过程解析
Dapr source code analysis | project overview
ASEMI的MOS管9N90参数,9N90电路图,9N90实物图
Usage of dac8560
R language uses AOV function to perform two-way factorial ANOVA, and the plotmeans function of gplots package displays interaction in two-way ANOVA, including mean, error bar, 95% confidence interval
中途离开电脑怎么一键锁屏
30岁 思维导图
若依 思维导图
Where to open an account for futures and who to open it for safe and reliable??
检测ip地址库内是否存在指定的ip
JPEX推出BAYC MAYC合约系列 欢迎体验
办公桌子和椅子的选择和注意点
Redis实现分页