我需要使用PHP DateTime来获取当前年份的第一天。我试过:
$year = new DateTime('first day of this year');
var_dump($year);
但这似乎在本月的第一天又回来了:2014-09-01 09:28:56
为什么?如何正确获取当前年份的第一天?
由于first day of
:的定义,您的相对日期格式'first day of this year'
返回月的第一天是正确的
设置当前月份的第一天。这个短语是最好的与后面的月份名称一起使用。(参见PHP文档)
要用相对格式获得今年的第一天,你可以使用这样的东西:
'first day of January ' . date('Y')
或者只在strtotime
函数中使用文本:
date('Y-m-d', strtotime('first day of january this year'));
echo date('l',strtotime(date('Y-01-01')));
您可以获取当前日期,然后将日期和月份设置为1:
$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') ));
只是想通过
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'));
这将计算到'first day of january 2018'
,但您实际上需要在美国/纽约的第一个日期,也就是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
中获得今年的界限
使用相对日期格式可以通过两种方式实现这一点。
一种方法是在对象上使用CCD_ 11函数。
$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]);
在这里试用:https://www.tehplayground.com/Qk3SkcrCDkNJoLK2
另一种方法是用逗号分隔相对字符串,如下所示:
$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]);
在这里试用:https://www.tehplayground.com/hyCqXLRBlhJbCyks
基本日期('Y')返回当前年份的值。每年的第一天是从2000-01-01开始的。
因此,这将帮助您获得准确的输出。
$firstdate = date( 'Y' ) . '-01-01';
看看这个链接——http://davidhancock.co/2013/11/get-the-firstlast-day-of-a-week-month-quarter-or-year-in-php/
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");
在PHP 5.4及更高版本中,您可以将所有放在一起
echo (new 'DateTime(date("Y")."-01-01"))->format("Y-m-d")
正如一位评论者@Glavić所说,
$year = new DateTime('first day of January');
是解决方案。
对我来说,"今年"应该回到一年中第一天的午夜,这在语义上确实有意义,但事实并非如此!
下面是获取一年中第一天和最后一天的代码片段。
$firstDayOfYear = date('Y-01-01');
$lastDayOfYear = date('Y-12-t');
试试这个:
$dt = date('m/d/Y',time());
echo 'First day : '. date("01/01/Y", strtotime($dt)).' - Last day : '. date("m/t/Y", strtotime($dt));
使用DateTime对象获取当前年份的第一天和最后一天。
$currentDate = new 'DateTime("NOW");
$yearStartString = $currentDate->format("Y")."-01-01 00:00:00";
$yearEndString = $currentDate->format("Y")."-12-31 23:59:59";
$yearStartDate = 'DateTime::createFromFormat("Y-m-d H:i:s", $yearStartString);
$yearEndDate = 'DateTime::createFromFormat("Y-m-d H:i:s", $yearEndString);