Php 特定年份中两个日期之间的五月天是多少
我正在解决以下任务> 我有两个日期-$start和$end,目标年为$year 日期是php日期时间对象,年份是字符串 添加:日期来自MySql字段,格式为2017-02-01 15:00:00。。。 地址2:如果结束日期为空,我使用今天的日期 我需要计算出特定年份的这两个日期之间有多少天 我还需要一整天的时间,即使一天中的一分钟应该算作一整天 我可以通过许多下面的假设来解决它 我在示例中使用的值的预期结果如下 2016年是0天 2017年是31天 2018年是32天 2019年是0天 但是有什么优雅的php函数可以帮我解决这个问题吗? 我所做的似乎是错误的方式,并给出了糟糕的结果-似乎它只计算全天 请在此处查看我的代码>Php 特定年份中两个日期之间的五月天是多少,php,datetime,Php,Datetime,我正在解决以下任务> 我有两个日期-$start和$end,目标年为$year 日期是php日期时间对象,年份是字符串 添加:日期来自MySql字段,格式为2017-02-01 15:00:00。。。 地址2:如果结束日期为空,我使用今天的日期 我需要计算出特定年份的这两个日期之间有多少天 我还需要一整天的时间,即使一天中的一分钟应该算作一整天 我可以通过许多下面的假设来解决它 我在示例中使用的值的预期结果如下 2016年是0天 2017年是31天 2018年是32天 2019年是0天 但是有什
<?php
$diff = True;
$start = DateTime::createFromFormat('Y-m-d H:i:s','2017-12-01 23:05:00');
$end = DateTime::createFromFormat('Y-m-d H:i:s','2017-12-03 00:05:00');
$year = '2017';
// start date
if ($start->format('Y')<$year)
{
$newstart = new DateTime('first day of January '. $year);
}
if ($start->format('Y')==$year)
{
$newstart = $start;
}
if ($start->format('Y')>$year)
{
$result = 0;
$diff = False;
}
// end date
if ($end->format('Y')>$year)
{
$newend = new DateTime('last day of December '. $year);
}
if ($end->format('Y')==$year)
{
$newend = $end;
}
if ($end->format('Y')<$year)
{
$result = 0;
$diff = False;
}
// count if diff is applicable
if ($diff)
{
$result = $newend->diff($newstart)->format("%a");
}
echo $result;
?>
只要确保只包含日期部分,就不必处理舍入问题
但是有什么优雅的php函数可以帮我解决这个问题吗
了解。它返回一个对象,该对象包含以$days为单位的天数,通过检查$h、$i和$s的值,可以判断是否必须将其递增以对结果进行四舍五入。您还可以使用和将时间间隔裁剪到所需的年份
function getDays(DateTimeInterface $start, DateTimeInterface $end, $year)
{
// Extend the start date and end date to include the entire day
$s = clone $start; // Don't modify $start and $end, use duplicates
$s->setTime(0, 0, 0);
$e = clone $end;
$e->setTime(0, 0, 0)->add(new DateInterval('P1D')); // start of the next day
// Crop the input interval to the desired year
$s = min($s, new DateTime("$year-01-01 00:00:00"));
$year ++;
$e = max(new DateTime("$year-01-01 00:00:00"), $end); // start of the next year
if ($e <= $s) {
// The input interval does not span across the desired year
return 0;
}
// Compute the difference and return the number of days
$diff = $e->diff($s);
return $diff->days;
}
由于四舍五入的原因,不完全是重复的,但是这可以让您开始学习,但是有什么优雅的php函数可以帮助我完成这项工作吗了解。它返回一个对象,该对象包含以$days为单位的天数,通过检查$h、$i和$s的值,可以判断是否必须将其递增以对结果进行四舍五入。您还可以使用和将时间间隔裁剪到所需的年份。谢谢您的回答。如您所见,我已经使用了DateTime:diff;我舍入了输入,所以它只是日期,忽略了时间。我想知道的是,它总是少返回一天,比如2017-12-01到2017-12-05是5天,但它总是少返回4天。也许我误解了“差异”这个词,但我需要这些天之间的总天数包括这些天。解决方案似乎是在计算结束时加上+1?你提到最小值和最大值,似乎这可以避免我愚蠢的假设,但我无法找到如何裁剪这个间隔,你能给我一个简短的例子吗?嗨,thx的建议。它没有反映的是需要指定年份。。。因此,对于不同年份的日期,它将不起作用,最好说它将返回介于和之间的总天数,而不是指定年份的天数。您尝试过该代码吗?它肯定会在不同的年份发挥作用。我将d1美元改为“2016-05-15”,并获得了381天的回报,即365+16。不过,你可能对1970年之前的日期有一些问题。指定年份的天数-哪一年?这是另一种计算。
function getDays(DateTimeInterface $start, DateTimeInterface $end, $year)
{
// Extend the start date and end date to include the entire day
$s = clone $start; // Don't modify $start and $end, use duplicates
$s->setTime(0, 0, 0);
$e = clone $end;
$e->setTime(0, 0, 0)->add(new DateInterval('P1D')); // start of the next day
// Crop the input interval to the desired year
$s = min($s, new DateTime("$year-01-01 00:00:00"));
$year ++;
$e = max(new DateTime("$year-01-01 00:00:00"), $end); // start of the next year
if ($e <= $s) {
// The input interval does not span across the desired year
return 0;
}
// Compute the difference and return the number of days
$diff = $e->diff($s);
return $diff->days;
}