Getting Last Month's Date in PHP

Getting last month's date in php

It's simple to get last month date

echo date("Y-n-j", strtotime("first day of previous month"));
echo date("Y-n-j", strtotime("last day of previous month"));

at November 3 returns

2014-10-1
2014-10-31

How to find the last day of the month from date?

t returns the number of days in the month of a given date (see the docs for date):

$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));

Get the last day of the last month and the first day of next month in PHP

Use below code:

<?php

$month_end = new DateTime("last day of last month");
$month_ini = new DateTime("first day of next month");

echo $month_end->format('Y-m-d'); // will print, Last day of last month
echo $month_ini->format('Y-m-d'); // will print First day of next month

?>

With Datetime object with user defined date (Custom date)

# with datetime object
$d1 = new DateTime('2019-12-01');
$d1 -> modify('last day of last month');
echo $d1 -> format('d.m.Y'), "\n";

$d2 = new DateTime('2019-12-01');
$d2 -> modify('first day of next month');
echo $d2 -> format('d.m.Y'), "\n";

Output:
30.11.2019
01.01.2020

How can I find the first and last date in a month using PHP?

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 http://php.net/manual/en/function.date.php for other symbols and more details.

How to get previous month and year relative to today, using strtotime and date?

Have a look at the DateTime class. It should do the calculations correctly and the date formats are compatible with strttotime. Something like:

$datestring='2011-03-30 first day of last month';
$dt=date_create($datestring);
echo $dt->format('Y-m'); //2011-02

get first day of last month result changes

You need to set time explicitly:

$monthStart = (new DateTime("first day of last month"))->setTime(0,0,0);
$monthEnd = (new DateTime("last day of last month"))->setTime(23,59,59);

By default it returns current time.

php get last day of given month

I have had this problem with PHP before, try the following way:

$dateToTest = "2015-02-01";
$lastday = date('t',strtotime($dateToTest));


Related Topics



Leave a reply



Submit