61

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
  • 741
  • 1
  • 6
  • 26
Oscar
  • 1,093
  • 4
  • 21
  • 31

4 Answers4

188

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

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
10
date('d-m-Y', strtotime(date('Y-m-1')));
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 11
    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
10
$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
  • 53,206
  • 45
  • 230
  • 366