• 3
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I need to get the first and last day of a month in the format YYYY-MM-DD given only the month and year. Is there a good, easy way to do this?

php

First day is always YYYY-MM-01, isn't it? Example: date("Y-M-d", mktime(0, 0, 0, 8, 1, 2008))

Last day is the previous day of the next month's first day:

$date = new DateTime("2008-09-01");
$date->modify("-1 day");
echo $date->format("Y-m-d");
  • 6
Reply Report

The first day of the month is always 1. So it will become

YYYY-MM-01

the last day can be calculated as:

<?php
    $num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
    echo "There was $num days in August 2003";
?>
  • 3
Reply Report

OK, first is dead easy.

date ('Y-m-d', mktime(0,0,0,MM,01,YYYY));

Last is a little trickier, but not much.

date ('Y-m-d', mktime(0,0,0,MM + 1,-1,YYYY));

If I remember my PHP date stuff correctly...

**edit - Gah! Beaten to it about a million times...

Edit by Pat:

Last day should have been

date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1
  • 2
Reply Report
<?php
echo "Month Start - " . $monthStart = date("Y-m-1") . "<br/>";
$num = cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y"));
echo "Monthe End - " . $monthEnd = date("Y-m-".$num);
?>
  • 2
Reply Report

The easiest way to do this with PHP is

$dateBegin = strtotime("first day of last month");  
$dateEnd = strtotime("last day of last month");

echo date("MYDATEFORMAT", $dateBegin);  
echo "<br>";        
echo date("MYDATEFORMAT", $dateEnd);

Or the last week

if (date('N', time()) == 7) {
$dateBegin = strtotime("-2 weeks Monday");
$dateEnd = strtotime("last Sunday");
} else {
$dateBegin = strtotime("Monday last week"); 
$dateEnd = strtotime("Sunday last week");   
}

Or the last year

$dateBegin = strtotime("1/1 last year");
$dateEnd = strtotime("12/31 this year");
  • 1
Reply Report

By the way @ZombieSheep solution

date ('Y-m-d', mktime(0,0,0,$MM + 1,-1,$YYYY));

does not work it should be

date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1

Of course @Micha? S?aby's accepted solution is the simplest.

  • 0
Reply Report

Just to verify that I didn't miss any loose ends:

$startDay = 1;

if (date("m") == 1) {
    $startMonth = 12;
    $startYear = date("Y") - 1;

    $endMonth = 12;
    $endYear = date("Y") - 1;
}
else {
    $startMonth = date("m") - 1;
    $startYear = date("Y");

    $endMonth = date("m") - 1;
    $endYear = date("Y");
}

$endDay = date("d") - 1;

$startDate = date('Y-m-d', mktime(0, 0, 0, $startMonth , $startDay, $startYear));
$endDate = date('Y-m-d', mktime(0, 0, 0, $endMonth, $endDay, $endYear));
  • 0
Reply Report

Example; I want to get first day and last day of current month.

$month   = (int) date('F');
$year    = (int) date('Y');

date('Y-m-d', mktime(0, 0, 0, $month + 1, 1, $year)); //first
date('Y-m-d', mktime(0, 0, 0, $month + 2, 0, $year)); //last

When you run this for instance at date 2015-01-09, the first and last values will be sequentially;

2015-01-01
2015-01-31

Tested.

  • 0
Reply Report

From here(get next month last day) that is marked as duplicated, so i can't add comment there, but people can got bad answers from there.

Correct one for last day of next month:

echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-t'));

Correct one for first day of next month:

echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-01'));

Code like this will be providing March from January, so that's not what could be expected.

echo ((new DateTime())->modify('+1 month')->format('Y-m-t'));

  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions