183

I can get the Monday of this week with:

$monday = date_create()->modify('this Monday');

I would like to get with the same ease the 1st of this month. How can I achieve that?

MSeifert
  • 145,886
  • 38
  • 333
  • 352
pihentagy
  • 5,975
  • 9
  • 39
  • 58
  • Uh, I am in a trouble, John Conde answered the question I answered, but I forget to mention, that it needs to work on 5.2, so I will actually use Mr-sk's answer... – pihentagy Jan 20 '10 at 09:53

13 Answers13

407

Here is what I use.

First day of the month:

date('Y-m-01');

Last day of the month:

date('Y-m-t');
Etienne Dupuis
  • 13,548
  • 6
  • 47
  • 58
  • 12
    This is IMO by far the simplest and most readable way to solve this, if all you need is a string representation of the date. – Markus Amalthea Magnuson Aug 17 '12 at 11:23
  • 6
    Last day of the month then could be grabbed using `date('Y-m-t');`. – Mark Tomlin Jun 29 '13 at 13:38
  • 7
    FYI: The 't' in date('Y-m-t') always represents the number of days in that very month. A great and simple way to get the last day of the current month. – Pascal Klein Dec 15 '13 at 16:39
  • 1
    If I use this to try to get the day of the week `date('w', '2015-05-01');` then I end up with the day before instead of the actual day. (This would give me 4 instead of the correct 5). – PBwebD May 15 '15 at 05:17
  • 7
    This is simply brilliant. I expanded on it a little to give the min and max boundaries for time in the month by doing: `$monthStart = date('Y-m-01 00:00:00'); $monthEnd = date('Y-m-t 23:59:59');` – H2ONOCK Jun 09 '15 at 08:15
  • And don't forget that, should you need the unix timestamp, you can always do `strtotime(date('01-m-Y'));` – Erenor Paz Feb 25 '16 at 17:27
  • I don't often see PHP code that makes me question my entire career. But you, sir, have a brilliant piece of code here. Well done. No more convoluted `mktime` or `strtotime` for me. – Benjam Jun 14 '17 at 16:23
326

Requires PHP 5.3 to work ("first day of" is introduced in PHP 5.3). Otherwise the example above is the only way to do it:

<?php
    // First day of this month
    $d = new DateTime('first day of this month');
    echo $d->format('jS, F Y');

    // First day of a specific month
    $d = new DateTime('2010-01-19');
    $d->modify('first day of this month');
    echo $d->format('jS, F Y');
    
    // alternatively...
    echo date_create('2010-01-19')
      ->modify('first day of this month')
      ->format('jS, F Y');
    

In PHP 5.4+ you can do this:

<?php
    // First day of this month
    echo (new DateTime('first day of this month'))->format('jS, F Y');

    echo (new DateTime('2010-01-19'))
      ->modify('first day of this month')
      ->format('jS, F Y');

If you prefer a concise way to do this, and already have the year and month in numerical values, you can use date():

<?php
    echo date('Y-m-01'); // first day of this month
    echo "$year-$month-01"; // first day of a month chosen by you
N69S
  • 16,110
  • 3
  • 22
  • 36
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 10
    Note that you don't have to go through `->modify()` if you just want the first day of the **current month**. You can pass `'first day of this month'` to the DateTime constructor directly like: `$date = new DateTime('first day of this month')`. Just saying... – Dzhuneyt Jan 29 '15 at 09:52
  • Also i use: ```$date = (new \DateTime())->modify("first day of $month month"); dd($date->format('Y-m-d'), $date->format('Y-m-t'));``` where $month between -1 and 4, and it work (first day of 0 month - current month, first day of 1 month - next..) – Rijen Jun 06 '23 at 23:45
38

This is everything you need:

$week_start = strtotime('last Sunday', time());
$week_end = strtotime('next Sunday', time());

$month_start = strtotime('first day of this month', time());
$month_end = strtotime('last day of this month', time());

$year_start = strtotime('first day of January', time());
$year_end = strtotime('last day of December', time());

echo date('D, M jS Y', $week_start).'<br/>';
echo date('D, M jS Y', $week_end).'<br/>';

echo date('D, M jS Y', $month_start).'<br/>';
echo date('D, M jS Y', $month_end).'<br/>';

echo date('D, M jS Y', $year_start).'<br/>';
echo date('D, M jS Y', $year_end).'<br/>';
kaleazy
  • 5,922
  • 2
  • 47
  • 51
  • 6
    This is a neat solution, however `strtotime('first day of this month', time())` will return the first day, and the time is same as current. So there for, its not first day at 0:00 o'clock, its first day and as the time is now. This fixes it `strtotime('first day of this month 00:00:00', time())`. – Kalle H. Väravas Sep 01 '14 at 06:36
24

Currently I'm using this solution:

$firstDay = new \DateTime('first day of this month');
$lastDay = new \DateTime('last day of this month');

The only issue I came upon is that strange time is being set. I needed correct range for our search interface and I ended up with this:

$firstDay = new \DateTime('first day of this month 00:00:00');
$lastDay = new \DateTime('first day of next month 00:00:00');
Nikola Petkanski
  • 4,724
  • 1
  • 33
  • 41
  • This is cool, but I'm having some issues on the actual First day of the month. So, if today is the 1st of September, 11am. I will get 1st of August! I need to actually check if it's the "1st" today and if so use "now". weird ( Php 7.3.9 ) – Jacob Christensen Sep 16 '19 at 14:04
  • @JacobChristensen haven't experienced that yet. How did you fixed it? – Nikola Petkanski Oct 08 '19 at 06:46
  • Very true, php seams not to be very cool about this First and last. $n = new \DateTime('first day of ' . (new \DateTime())->format('F')); "first day of January" . Same happens for the Week when it's Monday and it's a bit... frustrating :) – Jacob Christensen Feb 11 '20 at 15:04
23

I use a crazy way to do this is using this command

$firstDay=date('Y-m-d',strtotime("first day of this month"));
$lastDay=date('Y-m-d',strtotime("last day of this month"));

Thats all

  • You need to stick strtotime("today") in the first part to ensure the time starts at 00:00:00 otherwise, you get the time it is now as the time on the first day of the month. See my answer but in short date("Y-m-d",strtotime("first day of this month",strtotime("today")); Then last day needs to be what you've done but using $firstDay as the "NOW" and then minus -1 second from it – Watts Epherson May 20 '21 at 20:49
8

In php 5.2 you can use:

<? $d = date_create();
print date_create($d->format('Y-m-1'))->format('Y-m-d') ?>
pihentagy
  • 5,975
  • 9
  • 39
  • 58
4

Ugly, (and doesn't use your method call above) but works:

echo 'First day of the month: ' . date('m/d/y h:i a',(strtotime('this month',strtotime(date('m/01/y')))));   
mr-sk
  • 13,174
  • 11
  • 66
  • 101
  • out of the topic but how would you calculate the last day of the month under the same scenario. – LoneWOLFs Dec 10 '12 at 12:28
  • @LoneWOLFs `last day of {$month} {$year}` or `last day of this month` both work. But keep in mind that $month would have to be a string containing the name of the month like "January" all the way thought to "Decemember". Or you could simply use `date('Y-m-t');`. – Mark Tomlin Jun 29 '13 at 13:36
  • "Decemember" :D – zanderwar Feb 18 '21 at 00:33
3

You can do it like this:

$firstday = date_create()->modify('first day January 2010');
Thomas Müller
  • 1,433
  • 11
  • 24
3

using date method, we should be able to get the result. ie; date('N/D/l', mktime(0, 0, 0, month, day, year));

For Example

echo date('N', mktime(0, 0, 0, 7, 1, 2017));   // will return 6
echo date('D', mktime(0, 0, 0, 7, 1, 2017));   // will return Sat
echo date('l', mktime(0, 0, 0, 7, 1, 2017));   // will return Saturday
Jacob Nelson
  • 2,370
  • 23
  • 35
3

I use this with a daily cron job to check if I should send an email on the first day of any given month to my affiliates. It's a few more lines than the other answers but solid as a rock.

//is this the first day of the month?
$date = date('Y-m-d');
$pieces = explode("-", $date);
$day = $pieces[2];

//if it's not the first day then stop
if($day != "01") {

     echo "error - it's not the first of the month today";
     exit;

}
Craig Edmonds
  • 2,052
  • 3
  • 14
  • 13
2

Timestamp for start of this month and very last second of current month. You can add 00:00:00 or just reference "today"

Alternative:

$startOfThisMonth = strtotime("first day of this month",strtotime("today"));
OR
$startOfThisMonth = strtotime("first day of this month 00:00:00");

$endOfThisMonth = strtotime("first day of next month",$startOfThisMonth)-1;
Watts Epherson
  • 692
  • 5
  • 9
0

I am providing this answer as an alternative one liner if the DateTime object is not preferred

Basically, I get the current day number, reduce it by one then take that number of days from itself ("today" which automatically resets the clock to 00:00:00 too) and you get the start of the month.

$startOfMonth = strtotime("today - ".(date("j")-1)." days");
Watts Epherson
  • 692
  • 5
  • 9
-1

If you're using composer, you can install carbon: composer require nesbot/carbon

This is then as simple as:

use Carbon/Carbon;

$startOfMonth = Carbon::now()->startOfMonth()->toDateTime();
Simon Epskamp
  • 8,813
  • 3
  • 53
  • 58