186

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?

1
  • 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, 2010 at 9:53

13 Answers 13

411

Here is what I use.

First day of the month:

date('Y-m-01');

Last day of the month:

date('Y-m-t');
7
  • 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. Aug 17, 2012 at 11:23
  • 6
    Last day of the month then could be grabbed using date('Y-m-t');. Jun 29, 2013 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. Dec 15, 2013 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, 2015 at 5: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 9, 2015 at 8:15
330

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
2
  • 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, 2015 at 9: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 6 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/>';
1
  • 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()). Sep 1, 2014 at 6:36
25

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');
3
  • 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 ) Sep 16, 2019 at 14:04
  • @JacobChristensen haven't experienced that yet. How did you fixed it? Oct 8, 2019 at 6: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 :) Feb 11, 2020 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

1
  • 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 May 20, 2021 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') ?>
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')))));   
3
  • out of the topic but how would you calculate the last day of the month under the same scenario.
    – LoneWOLFs
    Dec 10, 2012 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');. Jun 29, 2013 at 13:36
  • "Decemember" :D
    – zanderwar
    Feb 18, 2021 at 0:33
3

You can do it like this:

$firstday = date_create()->modify('first day January 2010');
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
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;

}
2
  • I can't believe that instead of the first 3 lines you simply didn't use $day = $date('d'); (!)
    – dev_masta
    Apr 20, 2021 at 3:29
  • @dev_masta yes it's true. Thanks for the tip. I could probably do if($date('d') != 01) to make it even shorter. Apr 20, 2021 at 9:55
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;
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");
0

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();
2
  • Question is about native PHP datetime functionality, suggesting different libs is not a good idea May 5 at 15:18
  • Simon answered to people wich is using Composer and that's my case. Upvoted.
    – Nowdeen
    Oct 15 at 19:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.