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 the first and last days of a given month
You might want to look at the strtotime
and date
functions.
<?php
$query_date = '2010-02-04';
// First day of the month.
echo date('Y-m-01', strtotime($query_date));
// Last day of the month.
echo date('Y-m-t', strtotime($query_date));
The best way to get the first and last day of last month?
In PHP 5.3, you can use the DateTime
class :
<?php
$month_ini = new DateTime("first day of last month");
$month_end = new DateTime("last day of last month");
echo $month_ini->format('Y-m-d'); // 2012-02-01
echo $month_end->format('Y-m-d'); // 2012-02-29
In PHP, is there an easy way to get the first and last date of a month?
$first = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
$last = date('Y-m-t', mktime(0, 0, 0, $month, 1, $year));
See date() in PHP documentation.
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
Getting first day of a given month in PHP?
The strtotime
function supports relative time formats. You can just do this instead:
$date = strtotime('20140702');
$first_date = strtotime('first day of this month', $date);
$first_day_of_month = date('w', $first_date);
The second parameter to strtotime
provides the time that the relative format will be relative to. You can use this to easily calculate dates relative to a particular point, as shown above.
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.
Retrieve the first day of the last month for a given date
Here's how to do this for any month, instead of the current month
$date = new DateTime('2016-05-25');
$date->modify('-1 month');
echo $date->format('Y-m-t'); // 2016-04-30
echo $date->format('Y-m-01'); // 2016-04-01
Related Topics
Simple Comet Example Using PHP and Jquery
What Is 22527 in Error_Reporting 22527 of PHPinfo
Get Start and End Days for a Given Week in PHP
How to Get the Last Path in a Url
PHP & MySQL: When Exactly to Use HTMLentities
PHP - How to Implement Password Reset and Token Expiry
How to Convert Object into String in PHP
How to Select Rows Where Column Value Is Not Null Using Codeigniter's Activerecord
Difference Between Eloquent\Model::Get() and All()
Change Name of Laravel's Created_At and Updated_At
Php: Get List of All Filenames Contained Within My Images Directory
Move All Files in a Folder to Another
How to Create a New Joomla User Account from Within a Script
Differencebetween Null and Empty
Message: Trying to Access Array Offset on Value of Type Null
MySQL Alter Table Add Field Before or After a Field Already Present