I need to use PHP DateTime to get the first day of the current year. I\'ve tried:
$year = new DateTime(\'first day of this year\');
var_dump($year);
<
Basically date('Y') returns the value of current year. and the first day of each year starts like 2000-01-01.
so this will help you to get exact output.
$firstdate = date('Y').'01-01';
As a commenter @Glavić said already
$year = new DateTime('first day of January');
is the solution.
To me it did make sense semantically that "this year" should return midnight of the first day of the year, but indeed it does not!
Just wanted to record some nuance with the answer by
lorem monkey
The suggested method might cause issues with when using time zones.
scenario: consider current system time is 2018-01-01 00:20:00 UTC
and system default time zone is set to UTC.
date('Y')
will give you 2018
if you are doing something like:
$startDate = new DateTime('first day of january '.date('Y'), new DateTimeZone('America/New_York'));
This will compute to 'first day of january 2018'
but you actually needed the first date of your current year in America/New_York, which is still 2017.
Stop dropping the ball man, its not new year yet!
So it is better to just do
$startDate = new DateTime('first day of january', new DateTimeZone('America/New_York'));
And then do the modifications as needed by using the DateTime::modify()
function.
Relative date formats are fun.
My scenario: I wanted to get the bounds of this year as in
2018-01-01 00:00:00
to2018-12-31 23:59:59
This can be achieved in two ways with relative date formats.
one way is to use the DateTime::modify()
function on the object.
$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]);
Try out here: https://www.tehplayground.com/Qk3SkcrCDkNJoLK2
Another way to do this is to separate the relative strings with a comma like so:
$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]);
Try out here: https://www.tehplayground.com/hyCqXLRBlhJbCyks
Take a look at this link -- 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;
}
Or use only text in the strtotime
function:
date('Y-m-d', strtotime('first day of january this year'));
in PHP 5.3.10 this works
$myDate = new \DateTime(date("Y")."-01-01");
echo $myDate->format("Y-m-d");
In PHP 5.4 and upper you can put all together
echo (new \DateTime(date("Y")."-01-01"))->format("Y-m-d")