比较格式1年2个月3周4天的日期

技术标签: php

在我正在工作的项目中,我的年龄像

1Year 2Months 3Weeks 4Days

我正在尝试进行比较

如果年龄小于8周,那是 baby 如果年龄小于或等于12个月/1年,那就是 young 如果大于1年,那是 adult。我尝试的代码在下面给出。令人困惑。

有没有办法使用内置功能中的一些方法来简化这一点?

function getAgeGroup($age_years, $age_months, $age_weeks, $age_days) {
$age_group = 'baby';
if (!$age_years && !$age_months && (!$age_weeks || ($age_weeks <= 8 && (!$age_days || $age_days < 1)))) {
  $age_group = 'baby';
}
elseif (!$age_years && ($age_months == 1 || $age_months == 2) && (!$age_weeks && !$age_days)) {
  $age_group = 'baby';
}
elseif (!$age_years && $age_months == 1 && $age_weeks < 4 && $age_days) {
  $age_group = 'young';
}
elseif (!$age_years && $age_months == 1 && $age_weeks > 4) {
  $age_group = 'young';
}
elseif (!$age_years && ($age_months >= 2 && $age_months < 12) && ($age_weeks > 0 || $age_days > 0)) {
  $age_group = 'young';
}
elseif (!$age_years && ($age_months > 0 && $age_months <= 12) && ($age_weeks >= 8 && $age_days > 1)) {
  $age_group = 'young';
}
elseif (!$age_years && ($age_months > 2 && $age_months <= 12) && (!$age_weeks && !$age_days)) {
  $age_group = 'young';
}
elseif ($age_years == 1 && !$age_months && !$age_weeks && !$age_days) {
  $age_group = 'young';
}
elseif (($age_years > 0 || $age_months >= 12) && ($age_weeks > 0 || $age_days > 0)) {
  $age_group = 'adult';
}
elseif (($age_years > 0 || $age_months > 0)) {
  $age_group = 'adult';
}
elseif ($age_years > 1) {
  $age_group = 'adult';
}
return $age_group;
}

看答案

您可以尝试这样的事情: 评估 使用 DateTimeDateInterval 课程。


智能推荐

很多.net 程序员不知道又非常重要的 .net高级调试技巧.调试别人的dll方法内的变量...

事情是这样的, 最近需要开发Orcale的数据库. 于是使用了EF 加上 Oracle.ManagedDataAccess.Client 这个Oracle.ManagedDataAccess 很好用, 不需要orcale 客户端就能用. 但是这个类库有个地方不是非常好用. 数据读取出了问题,它的提示非常非常让人气愤. 啥调试信息都没有..只有一个提示 “类型转换异常”. 大...

Spring框架

了解Spring框架 好处 : 1.不依赖于应用服务器. 2.IOC(控制反转)技术实现组件控制. 通过DI(依赖注入)技术提高了组件之间的解耦. 3.通过AOP(面向切面编程)技术实现安全、事务、日志功能的统一管理, 提高复用. 4.Spring框架可以几乎与其他任何框架进行良好的整合使用. 5.Spring所有项目之间不相互依赖. IOC : 控制反转. 本质: 将手动创建对象的工作,交给Sp...

计算机网络简要框架

物理层 **、维持、关闭通信端点之间的机械特性、电气特性、功能特性以及过程特性。该层为上层协议提供了一个传输数据的可靠的物理媒体。简单的说,物理层确保原始的数据可在各种物理媒体上传输。物理层记住两个重要的设备名称,中继器和集线器。 中继器: 又称转发器,原理是信号再生和还原,扩大网络传输距离。 仅作用于信号的电气部份,不管数据中是否有错误数据或不适合网段的数据。 中继器的两端既能连接相同的媒体又能...

解决SVN不显示绿色图标问题

修改注册表 Windows Explorer Shell支持Overlay Icon最多15个,Windows自身已经使用了4个,所以就只剩下了11个供我们使用。如果你之前安装了例如Groove这样的软件,那么可能我们可利用的就更少了,轮不到Tortoise了。像这样的情况,我们可以调整Tortoise图标名称的字母顺序,来提高Tortoise的优先位,因为Windows内部就是安装名称的字母顺序...

DataX篇—离线同步作业分析及JSON构建

DataX系列 [第一章 DataX篇—阿里开源ETL同步工具](https://blog.csdn.net/qq_33451695/article/details/115048824?spm=1001.2014.3001.5501). 离线同步作业分析及JSON构建 DataX系列 前言 一、思路 二、增量同步问题 触发器 双向同步循环问题 二、插件扩展 前言 因业务需要,存在数据源...

猜你喜欢

poj 3047(树的重心 裸模板)

Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders. Unfortunately, the structure ...

mybatis plus的条件构造器

我们在使用条件构造器的时候要使用QueryWrapper或者UpdateWrapper来充当条件语句来进行构造 QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类 用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where 条件...

使用eclipse开发sprintboot项目application.properties不提示的处理方法

使用eclipse开发sprintboot项目application.properties不提示 文件上没有叶子 选中application.properties 右键,open width -->spring properties Editor 原因分析:使用的spring tools suite 3.9.2 ,一直可以使用, 安装了Jboss Tools 导致不能使用,属性文件以jbos...

为什么要使用消息队列?

在实际的项目实践中,消息队列有的使用还是比较常用的,有时在想消息队列的好处是什么,使用MQ能带来什么好处。在说之前,目前市场主流的几种MQ(activeMQ,RabbitMQ,RocketMQ,Kafka),关于MQ的入门就不说了,可以找下教程,写个demo测试一下就好了,还是比较简单的。先上张图了解下每个MQ的区别  使用消息队列主要起到解耦,异步,削峰的目的。 1、解耦: 就是一个系...

CentOS7命令总结

一.文件操作    0.基本操作     touch、mkdir /var/temp/nginx -p(创建多级目录时)、     echo xxx    作用:显示一段内容。 例如:echo $JAVA_HOME     &nb...

问答精选

How to extract beta coefficients for interaction effect in R?

I am examining the interaction between a continuous variable (bloodq) and a categorical variable with three levels (ER, RB, and WB). In order to see how the betas differ across tissue types, I would l...

what is the difference between Flatten() and GlobalAveragePooling2D() in keras

I want to pass the output of ConvLSTM and Conv2D to a Dense Layer in Keras, what is the difference between using global average pooling and flatten Both is working in my case. That both seem to work d...

How to invoke a test step with inputs at runtime from groovy script in SOAP UI?

I am writing a validation groovy script for a test step, intended to test a SOAP Web Service. Now, I want to call the same test step, with different input value from the groovy script. Is it possible?...

Wicket pagestore results wrong page

I have a problem with my web application with wicket. I am using wicket 6.14. I can't say exactly what the problem is, but I can describe the problem. I am using a self written pagestore, which uses h...

Unity load files from outside of resources folder

In unity is it possible to load a resource that is out side of the resources folder. I want the user to be able to set a textAsset variable from a file outside of the Assets directory entirely. You ca...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答