当前位置:网站首页>Calculate the difference in days, months, and years between two dates in PHP
Calculate the difference in days, months, and years between two dates in PHP
2022-07-02 07:28:00 【Snow wind in the night sky】
Preface
Recently, in the process of developing a project, it is necessary to calculate the difference between the two dates 、 How many months 、 How many years? , A calculation method is summarized below , For your reference .
terms of settlement
/** * Calculate the difference between the two dates / month / year * @param string $start_time Start date * @param string $end_time End date * @param int $type type (1: return int ,2: Months difference ,3: The difference is years ) */
function get_count_form_date($start_time, $end_time, $type = 1)
{
// Calculate the difference of days
if ($type == 1) {
$datetime_start = new DateTime($start_time);
$datetime_end = new DateTime($end_time);
$days = $datetime_start->diff($datetime_end)->days;
return $days;
}
[$y1, $m1, $d1] = explode("-", $start_time);
[$y2, $m2, $d2] = explode("-", $end_time);
// Calculate the difference in months
if ($type == 2) {
if ($y1 == $y2) {
$month = $m2 - $m1;
} else {
$month = ($m2 - $m1) + ($y2 - $y1) * 12;
}
return $month;
}
// Calculate the difference in years
if ($type == 3) {
$year = $y2 - $y1;
return $year;
}
}
It's easy to call methods :
$start_time = '2020-09-12';
$end_time = '2021-07-01';
$type = 1;
get_count_form_date($start_time, $end_time, $type = 1)
According to type Value returns :292、10、1
边栏推荐
- MapReduce concepts and cases (Shang Silicon Valley Learning Notes)
- 软件开发模式之敏捷开发(scrum)
- 中年人的认知科普
- Take you to master the formatter of visual studio code
- [torch] some ideas to solve the problem that the tensor parameters have gradients and the weight is not updated
- CRP implementation methodology
- Classloader and parental delegation mechanism
- Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK
- sparksql数据倾斜那些事儿
- ORACLE EBS ADI 开发步骤
猜你喜欢
【信息检索导论】第一章 布尔检索
使用MAME32K进行联机游戏
Pratique et réflexion sur l'entrepôt de données hors ligne et le développement Bi
[introduction to information retrieval] Chapter 7 scoring calculation in search system
@Transitional step pit
Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK
SSM student achievement information management system
外币记账及重估总账余额表变化(下)
Two table Association of pyspark in idea2020 (field names are the same)
ssm超市订单管理系统
随机推荐
ERNIE1.0 与 ERNIE2.0 论文解读
Feeling after reading "agile and tidy way: return to origin"
Classloader and parental delegation mechanism
CSRF attack
Pratique et réflexion sur l'entrepôt de données hors ligne et le développement Bi
Illustration of etcd access in kubernetes
【Torch】最简洁logging使用指南
数仓模型事实表模型设计
Sparksql data skew
Practice and thinking of offline data warehouse and Bi development
How to efficiently develop a wechat applet
使用Matlab实现:幂法、反幂法(原点位移)
Two table Association of pyspark in idea2020 (field names are the same)
Oracle EBs and apex integrated login and principle analysis
oracle-外币记账时总账余额表gl_balance变化(上)
MySQL无order by的排序规则因素
离线数仓和bi开发的实践和思考
一份Slide两张表格带你快速了解目标检测
[introduction to information retrieval] Chapter 3 fault tolerant retrieval
外币记账及重估总账余额表变化(下)