Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the first day of the month of a specific month/year

Tags:

date

php

Is there a better way of returning the date for the first day of a specific month/year, than the following?

$month = date('m');
$year = date('Y');
$from = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
like image 408
xylar Avatar asked Mar 02 '12 15:03

xylar


2 Answers

This is not exactly computationally elegant, but I like it because it's so readable:

strtotime("first day of this month");
like image 96
menacingly Avatar answered Sep 30 '22 18:09

menacingly


Why so complicated?

date("Y-m-01")

This will work just fine ;)

(Don't worry, I kicked myself hard for wasting an hour messing with strtotime before realising I could do this!)

like image 44
Niet the Dark Absol Avatar answered Sep 30 '22 17:09

Niet the Dark Absol