60

What I want to do is get 1st date of current month

Here is how to get last day of the current month

date('d-m-Y', strtotime('last day of this month'))

I've tried to use this, but it didn't work for me

date('d-m-Y', strtotime('first day of this month'))    

Any idea how to solve my problem ?

ttkalec
  • 721
  • 1
  • 6
  • 26
Oscar
  • 1,083
  • 4
  • 21
  • 29

4 Answers4

178

date('01-m-Y') should do it ;)

Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566
10
date('d-m-Y', strtotime(date('Y-m-1')));
zerkms
  • 240,587
  • 65
  • 429
  • 525
  • 9
    Ew, converting a format to a date string to a number to another date string... This is almost as bad as the people who do `$("#"+$(this).attr('id'))` in jQuery... – Niet the Dark Absol May 20 '13 at 03:05
  • @Kolink: I'm sure the outer `date()` is used just to prove the result is correct, while the timestamp is the desired result – zerkms May 20 '13 at 03:06
  • 2
    Is this really that bad? Seems to work well if you used a date format variable (or constant) and have the need to do something like this. Then if you change your date format, you won't have to also change things like this. `$dateFormat = "d F, Y"; echo date($dateFormat, strtotime(date('Y-m-1')));` – Robert Sep 18 '14 at 23:18
  • 1
    Any solution using `strtotime()` is bad because that function gives the caller a lot of freedom and does a lot of guesswork to figure out what they were trying to say, and so is very slow – Josip Rodin Mar 22 '16 at 14:12
9
$firstDayUTS = mktime (0, 0, 0, date("m"), 1, date("Y"));
$lastDayUTS = mktime (0, 0, 0, date("m"), date('t'), date("Y"));

$firstDay = date("d-m-Y", $firstDayUTS);
$lastDay = date("d-m-Y", $lastDayUTS);
4

How about :

date('1-m-Y',strtotime('this month'));
Raptor
  • 51,208
  • 43
  • 217
  • 353