Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

php - How to get the first day of the current year?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...