Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first day of the current year?

Tags:

php

datetime

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?

like image 430
mtmacdonald Avatar asked Sep 18 '14 07:09

mtmacdonald


People also ask

How do I get the first day of the current year in SQL?

Simple Query: SELECT DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) — Instead of GetDate you can put any date.

How do you determine the first day of the month?

We can use the EOMONTH formula to find the first day of the month as well. EOMONTH returns the last day of a month from a date. Here, we use the EOMONTH function to go to the last day of the previous month. Then, we add 1 to get the first day of the current month.


2 Answers

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') 
like image 112
lorem monkey Avatar answered Sep 20 '22 04:09

lorem monkey


Or use only text in the strtotime function:

date('Y-m-d', strtotime('first day of january this year')); 
like image 39
Daan Avatar answered Sep 22 '22 04:09

Daan