Php get start date of month

How can I find the first and last date in a month using PHP? For example, today is April 21, 2010; I want to find April 1, 2010 and April 30, 2010.

S.L. Barth

8,08971 gold badges50 silver badges63 bronze badges

asked Apr 21, 2010 at 5:18

1

The easiest way is to use date, which lets you mix hard-coded values with ones extracted from a timestamp. If you don't give a timestamp, it assumes the current date and time.

// Current timestamp is assumed, so these find first and last day of THIS month
$first_day_this_month = date['m-01-Y']; // hard-coded '01' for first day
$last_day_this_month  = date['m-t-Y'];

// With timestamp, this gets last day of April 2010
$last_day_april_2010 = date['m-t-Y', strtotime['April 21, 2010']];

date[] searches the string it's given, like 'm-t-Y', for specific symbols, and it replaces them with values from its timestamp. So we can use those symbols to extract the values and formatting that we want from the timestamp. In the examples above:

  • Y gives you the 4-digit year from the timestamp ['2010']
  • m gives you the numeric month from the timestamp, with a leading zero ['04']
  • t gives you the number of days in the timestamp's month ['30']

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!

See //php.net/manual/en/function.date.php for other symbols and more details.

answered Jul 23, 2010 at 20:07

Nathan LongNathan Long

120k94 gold badges329 silver badges438 bronze badges

1

Simple one

  • Y - A full numeric representation of a year, 4 digits
  • m - Numeric representation of a month, with leading zeros
  • t - Number of days in the given month

Reference - //www.php.net/manual/en/function.date.php


answered Nov 10, 2010 at 5:11

1

You can use the date function to find how many days in a month there are.

// Get the timestamp for the date/month in question.
$ts = strtotime['April 2010'];

echo date['t', $ts]; 
// Result: 30, therefore, April 30, 2010 is the last day of that month.

Hope that helps.

EDIT: After reading Luis' answer, it occurred to me you may want it in the right format [YY-mm-dd]. It may be obvious, but doesn't hurt to mention:

// After the above code
echo date['Y-m-t', $ts]; 

answered Apr 21, 2010 at 5:27

Henrik L.Henrik L.

9521 gold badge8 silver badges10 bronze badges

This will give you the last day of the month:

function lastday[$month = '', $year = ''] {
   if [empty[$month]] {
      $month = date['m'];
   }
   if [empty[$year]] {
      $year = date['Y'];
   }
   $result = strtotime["{$year}-{$month}-01"];
   $result = strtotime['-1 second', strtotime['+1 month', $result]];
   return date['Y-m-d', $result];
}

And the first Day:

function firstDay[$month = '', $year = '']
{
    if [empty[$month]] {
      $month = date['m'];
   }
   if [empty[$year]] {
      $year = date['Y'];
   }
   $result = strtotime["{$year}-{$month}-01"];
   return date['Y-m-d', $result];
} 

answered Apr 21, 2010 at 5:27

LuisLuis

5,9492 gold badges30 silver badges51 bronze badges

1

For specific month and year use date[] as natural language as following

$first_date = date['d-m-Y',strtotime['first day of april 2010']];
$last_date = date['d-m-Y',strtotime['last day of april 2010']];
// Isn't it simple way?

but for Current month

$first_date = date['d-m-Y',strtotime['first day of this month']];
$last_date = date['d-m-Y',strtotime['last day of this month']];

answered Jul 29, 2015 at 5:33

If you want to find the first day and last day from the specified date variable then you can do this like below:

$date    =    '2012-02-12';//your given date

$first_date_find = strtotime[date["Y-m-d", strtotime[$date]] . ", first day of this month"];
echo $first_date = date["Y-m-d",$first_date_find];

$last_date_find = strtotime[date["Y-m-d", strtotime[$date]] . ", last day of this month"];
echo $last_date = date["Y-m-d",$last_date_find];

For the current date just simple use this

$first_date = date['Y-m-d',strtotime['first day of this month']];
$last_date = date['Y-m-d',strtotime['last day of this month']];

kelvin

1,3841 gold badge12 silver badges25 bronze badges

answered Feb 11, 2017 at 7:56

FaisalFaisal

4,3742 gold badges39 silver badges49 bronze badges

1

To get the first and last date of Last Month;

$dateBegin = strtotime["first day of last month"];  
$dateEnd = strtotime["last day of last month"];

echo date["D-F-Y", $dateBegin];  
echo "
"; echo date["D-F-Y", $dateEnd];

Shehary

9,86510 gold badges38 silver badges69 bronze badges

answered Jun 18, 2015 at 11:56

karuppubkaruppub

811 silver badge6 bronze badges

In simple format

   $curMonth = date['F'];
   $curYear  = date['Y'];
   $timestamp    = strtotime[$curMonth.' '.$curYear];
   $first_second = date['Y-m-01 00:00:00', $timestamp];
   $last_second  = date['Y-m-t 12:59:59', $timestamp]; 

For next month change $curMonth to $curMonth = date['F',strtotime["+1 months"]];

answered Oct 26, 2017 at 11:13

Yasar ArafathYasar Arafath

5851 gold badge11 silver badges29 bronze badges

$month=01;
$year=2015;
$num = cal_days_in_month[CAL_GREGORIAN, $month, $year];
echo $num;

display 31 last day of date

answered Dec 31, 2015 at 7:42

You can use DateTimeImmutable::modify[] :


$date = DateTimeImmutable::createFromFormat['Y-m-d', '2021-02-13'];

var_dump[$date->modify['first day of this month']->format['Y-m-d']]; // string[10] "2021-02-01"
var_dump[$date->modify['last day of this month']->format['Y-m-d']]; // string[10] "2021-02-28"

answered Jan 21, 2021 at 13:56

For those asking in the mysql context, if you have a $dateFrom like '2022-07-02' and want the first date, do this

date['Y-m-01', strtotime[ $dateFrom]]

You can do similar things for last date etc.

answered Jul 28 at 6:48

1

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

How to get 1st date of current month in php?

php echo 'First Date = ' . date['Y-m-01'] ..
Y gives you the 4-digit year from the timestamp ['2010'].
m gives you the numeric month from the timestamp, with a leading zero ['04'].
t gives you the number of days in the timestamp's month ['30'].

How can I get end of month in php?

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

How can I get last month start and end in php?

“get previous month start & end date in php” Code Answer's.

Chủ Đề