The first day of the current month in php using date_modify as DateTime object

PhpDateDatetime

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

Here is what I use.

First day of the month:

date('Y-m-01');

Last day of the month:

date('Y-m-t');

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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

Solution 5 - Php

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

Solution 6 - Php

In php 5.2 you can use:

<? $d = date_create();
print date_create($d->format('Y-m-1'))->format('Y-m-d') ?>

Solution 7 - Php

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

                                                                                               

Solution 8 - Php

You can do it like this:

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

Solution 9 - 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

Solution 10 - Php

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;
 
}

Solution 11 - Php

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;

Solution 12 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpihentagyView Question on Stackoverflow
Solution 1 - PhpEtienne DupuisView Answer on Stackoverflow
Solution 2 - PhpJohn CondeView Answer on Stackoverflow
Solution 3 - PhpkaleazyView Answer on Stackoverflow
Solution 4 - PhpNikola PetkanskiView Answer on Stackoverflow
Solution 5 - PhpJorge Ricardo Sanchez CocunuboView Answer on Stackoverflow
Solution 6 - PhppihentagyView Answer on Stackoverflow
Solution 7 - Phpmr-skView Answer on Stackoverflow
Solution 8 - PhpThomas MüllerView Answer on Stackoverflow
Solution 9 - PhpJacob NelsonView Answer on Stackoverflow
Solution 10 - PhpCraig EdmondsView Answer on Stackoverflow
Solution 11 - PhpAdam WhateversonView Answer on Stackoverflow
Solution 12 - PhpVadim SamokhinView Answer on Stackoverflow