How can i get first day of current month in php?

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

137k32 gold badges317 silver badges331 bronze badges

asked Jan 19, 2010 at 15:53

1

Here is what I use.

First day of the month:

date('Y-m-01');

Last day of the month:

date('Y-m-t');

answered Feb 23, 2012 at 14:17

Etienne DupuisEtienne Dupuis

13.1k6 gold badges46 silver badges56 bronze badges

7

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:

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:

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():

How can i get first day of current month in php?

N69S

13.8k3 gold badges18 silver badges33 bronze badges

answered Jan 19, 2010 at 16:23

John CondeJohn Conde

214k98 gold badges447 silver badges489 bronze badges

1

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).'
'; echo date('D, M jS Y', $week_end).'
'; echo date('D, M jS Y', $month_start).'
'; echo date('D, M jS Y', $month_end).'
'; echo date('D, M jS Y', $year_start).'
'; echo date('D, M jS Y', $year_end).'
';

answered Nov 19, 2012 at 22:33

kaleazykaleazy

5,5482 gold badges45 silver badges50 bronze badges

1

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');

answered Jul 4, 2012 at 7:31

Nikola PetkanskiNikola Petkanski

4,5841 gold badge32 silver badges41 bronze badges

3

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

answered Jul 18, 2014 at 16:34

1

In php 5.2 you can use:

format('Y-m-1'))->format('Y-m-d') ?>

answered Jan 20, 2010 at 10:06

pihentagypihentagy

5,7919 gold badges36 silver badges57 bronze badges

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')))));   

answered Jan 19, 2010 at 16:21

How can i get first day of current month in php?

mr-skmr-sk

12.9k9 gold badges62 silver badges99 bronze badges

3

You can do it like this:

$firstday = date_create()->modify('first day January 2010');

answered Jan 19, 2010 at 16:30

How can i get first day of current month in php?

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

answered Jul 20, 2017 at 11:28

Jacob NelsonJacob Nelson

2,18918 silver badges33 bronze badges

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;

}

answered Feb 13, 2019 at 9:01

Craig EdmondsCraig Edmonds

1,9263 gold badges13 silver badges13 bronze badges

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;

answered May 20, 2021 at 20:46

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");

answered Jun 3 at 22:28

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();

answered Jun 9 at 9:41

Simon EpskampSimon Epskamp

8,0262 gold badges52 silver badges56 bronze badges

All those special php expressions, in spirit of first day of ... are great, though they go out of my head time and again.

So I decided to build a couple of basic datetime abstractions and tons of specific implementation which are auto-completed by any IDE. The point is to find what-kind of things. Like, today, now, the first day of a previous month, etc. All of those things I've listed are datetimes. Hence, there is an interface or abstract class called ISO8601DateTime, and specific datetimes which implement it.

The code in your particular case looks like that:

(new TheFirstDayOfThisMonth(new Now()))->value();

For more about this approach, take a look at this entry.

answered May 9, 2020 at 13:23

How can i get first day of current month in php?

Vadim SamokhinVadim Samokhin

3,3044 gold badges39 silver badges66 bronze badges

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

How do you get the first day of the month in PHP?

php $dt = "2008-02-23"; echo 'First day : '. date("Y-m-01", strtotime($dt)).

How can get first day of current year in PHP?

Basically date('Y') returns the value of current year. and the first day of each year starts like 2000-01-01. So $firstdate = date('Y'). ... .
I've meant to explain it in your answer... You can edit it anytime! – FZs. ... .
for the current year 1st date this will do $startDate= date('Y-01-01'); – Kennedy Maikuma..

How can I get start date and end of month in PHP?

You can be creative with this. For example, to get the first and last second of a month: $timestamp = strtotime('February 2012'); $first_second = date('m-01-Y 00:00:00', $timestamp); $last_second = date('m-t-Y 12:59:59', $timestamp); // A leap year!

How do I get the last day of the current month in PHP?

$date = strtotime ( $datestring ); // Last date of current month. $day = date ( "l" , $lastdate );