How to get the first day of the current year?
PhpDatetimePhp Problem Overview
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);
But this seems to be returning the first day of the current month: 2014-09-01 09:28:56
Why? How do I correctly get the first day of the current year?
Php Solutions
Solution 1 - Php
Your relative date format 'first day of this year'
is correct by returning the first day of the month because of the definition of first day of
:
> Sets the day of the first of the current month. This phrase is best > used together with a month name following it. (See PHP-doc)
To get the first day of the current year with the relative format you can use something like this:
'first day of January ' . date('Y')
Solution 2 - Php
Or use only text in the strtotime
function:
date('Y-m-d', strtotime('first day of january this year'));
Solution 3 - Php
echo date('l',strtotime(date('Y-01-01')));
Solution 4 - Php
You can get the current date and then set day and month to 1:
$year = new DateTime();
$year->setDate($year->format('Y'), 1, 1);
Optionally, you can set the time to midnight:
$year->setTime(0, 0, 0);
Solution 5 - Php
If you want to get first day of current year just use this code
echo date("l", strtotime('first day of January '.date('Y') ));
Solution 6 - Php
> 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
to 2018-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
Solution 7 - Php
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';
Solution 8 - Php
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;
}
Solution 9 - Php
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")
Solution 10 - Php
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!
Solution 11 - Php
Try this:
$dt = date('m/d/Y',time());
echo 'First day : '. date("01/01/Y", strtotime($dt)).' - Last day : '. date("m/t/Y", strtotime($dt));
Solution 12 - Php
Following is the code snippet for getting first and last day of the year.
$firstDayOfYear = date('Y-01-01');
$lastDayOfYear = date('Y-12-t');