我需要使用PHP DateTime来获取当前年份的第一天。我已经尝试过:
$year = new DateTime('first day of this year');
var_dump($year);
但是这似乎返回的是当前月份的第一天:2014-09-01 09:28:56
为什么会这样?如何正确获取当前年份的第一天?
或者在 strtotime
函数中仅使用文本:
date('Y-m-d', strtotime('first day of january this year'));
'first day of this year'
是正确的,因为first day of
的定义是返回当前月份的第一天:
设置当前月份的第一天。最好与其后跟随的月份名称一起使用。(参见PHP文档)
要使用相对格式获取当前年份的第一天,可以使用以下代码:
'first day of January ' . date('Y')
echo date('l',strtotime(date('Y-01-01')));
$year = new DateTime();
$year->setDate($year->format('Y'), 1, 1);
可选的,您可以将时间设置为午夜:
$year->setTime(0, 0, 0);
如果您想获取当前年份的第一天,只需使用此代码
echo date("l", strtotime('first day of January '.date('Y') ));
基本上,date('Y') 返回当前年份的值。每年的第一天始于 2000-01-01。
因此,这将帮助您获得精确的输出结果。
$firstdate = date( 'Y' ) . '-01-01';
$firstdate = date('Y').'01-01';
返回当前年份的第一天的值。 - Rakibul Islam只是想记录一下
lorem monkey
的答案中的一些细微差别。
建议的方法可能会在使用时区时出现问题。
场景:假设当前系统时间为 2018-01-01 00:20:00 UTC
,系统默认时区设置为 UTC。
date('Y')
将给你2018
如果你正在做这样的事情:
$startDate = new DateTime('first day of january '.date('Y'), new DateTimeZone('America/New_York'));
这将计算为'2018年1月1日'
,但实际上您需要的是美国/纽约当前年份的第一个日期,即2017年。
别掉链子,伙计,现在还没到新年!
所以最好只需执行以下操作:
$startDate = new DateTime('first day of january', new DateTimeZone('America/New_York'));
DateTime::modify()
函数进行必要的修改。
相对日期格式很有趣。
我的情况是:我想要获取今年的范围,如
2018-01-01 00:00:00
到2018-12-31 23:59:59
有两种使用相对日期格式实现此目标的方法。
一种方法是在对象上使用DateTime::modify()
函数。
$startDate = (new DateTime('now', new DateTimeZone("America/New_York")));
$endDate = (new DateTime('now', new DateTimeZone("America/New_York")));
$startDate->modify("january")
->modify("first day of this month")
->modify("midnight");
$endDate->modify("next year")
->modify("january")
->modify("first day of this month")
->modify("midnight")->modify("-1 second");
var_dump([$startDate, $endDate]);
$startDate = (new DateTime('first day of january', new DateTimeZone("America/New_York")));
$endDate = (new DateTime('next year, first day of january, -1 second', new DateTimeZone("America/New_York")));
var_dump([$startDate, $endDate]);
$firstDayOfYear = date('Y-01-01');
$lastDayOfYear = date('Y-12-t');
date('Y')
返回当前年份。date('t')
返回当前月份的最后一天。 - Shamshad Zaheer function firstDayOf($period, DateTime $date = null)
{
$period = strtolower($period);
$validPeriods = array('year', 'quarter', 'month', 'week');
if ( ! in_array($period, $validPeriods))
throw new InvalidArgumentException('Period must be one of: ' . implode(', ', $validPeriods));
$newDate = ($date === null) ? new DateTime() : clone $date;
switch ($period) {
case 'year':
$newDate->modify('first day of january ' . $newDate->format('Y'));
break;
case 'quarter':
$month = $newDate->format('n') ;
if ($month < 4) {
$newDate->modify('first day of january ' . $newDate->format('Y'));
} elseif ($month > 3 && $month < 7) {
$newDate->modify('first day of april ' . $newDate->format('Y'));
} elseif ($month > 6 && $month < 10) {
$newDate->modify('first day of july ' . $newDate->format('Y'));
} elseif ($month > 9) {
$newDate->modify('first day of october ' . $newDate->format('Y'));
}
break;
case 'month':
$newDate->modify('first day of this month');
break;
case 'week':
$newDate->modify(($newDate->format('w') === '0') ? 'monday last week' : 'monday this week');
break;
}
return $newDate;
}
在 PHP 5.3.10 中,这是有效的。
$myDate = new \DateTime(date("Y")."-01-01");
echo $myDate->format("Y-m-d");
echo (new \DateTime(date("Y")."-01-01"))->format("Y-m-d")
new \DateTime('今年的1月1日')
或者new \DateTime('明年的12月31日')
。 - Tecnocat'今年的第一天'
返回一个有效的日期,而不是今年的第一天吗?这有意义吗? - algorhythm