How can I get the last day (Dec 31) of the current year as a date using PHP?
I tried the following but this doesn't work:
$year = date('Y');
$yearEnd = strtotime($year . '-12-31');
What I need is a date that looks like 2014-12-31 for the current year.
PHP strtotime() uses the current date/time as a basis (so it will use this current year), and you need date() to format:
$yearEnd = date('Y-m-d', strtotime('Dec 31'));
//or
$yearEnd = date('Y-m-d', strtotime('12/31'));
You can just concatenate actual year with required date
$year = date('Y') . '-12-31';
echo $year;
//output 2014-12-31
DateTime is perfectly capable of doing this:
$endOfYear = new \DateTime('last day of December this year');
You can even combine it with more modifiers to get the end of next year:
$endOfNextYear = new \DateTime('last day of December this year +1 years');
Another way to do this is use the Relative Formats of strtotime()
$yearEnd = date('Y-m-d', strtotime('last day of december this year'));
// or
$yearEnd = date('Y-m-d', strtotime('last day of december'));
Related
I need to use PHP DateTime to get the first day of the current year. I've tried:
$year = new DateTime('first day of this year');
var_dump($year);
But this seems to be returning the first day of the current month: 2014-09-01 09:28:56
Why? How do I correctly get the first day of the current year?
Your relative date format 'first day of this year' is correct by returning the first day of the month because of the definition of first day of:
Sets the day of the first of the current month. This phrase is best
used together with a month name following it. (See PHP-doc)
To get the first day of the current year with the relative format you can use something like this:
'first day of January ' . date('Y')
Or use only text in the strtotime function:
date('Y-m-d', strtotime('first day of january this year'));
echo date('l',strtotime(date('Y-01-01')));
You can get the current date and then set day and month to 1:
$year = new DateTime();
$year->setDate($year->format('Y'), 1, 1);
Optionally, you can set the time to midnight:
$year->setTime(0, 0, 0);
If you want to get first day of current year just use this code
echo date("l", strtotime('first day of January '.date('Y') ));
Just wanted to record some nuance with the answer by lorem monkey
The suggested method might cause issues with when using time zones.
scenario: consider current system time is 2018-01-01 00:20:00 UTC and system default time zone is set to UTC.
date('Y') will give you 2018
if you are doing something like:
$startDate = new DateTime('first day of january '.date('Y'), new DateTimeZone('America/New_York'));
This will compute to 'first day of january 2018' but you actually needed the first date of your current year in America/New_York, which is still 2017.
Stop dropping the ball man, its not new year yet!
So it is better to just do
$startDate = new DateTime('first day of january', new DateTimeZone('America/New_York'));
And then do the modifications as needed by using the DateTime::modify() function.
Relative date formats are fun.
My scenario: I wanted to get the bounds of this year as in 2018-01-01 00:00:00 to 2018-12-31 23:59:59
This can be achieved in two ways with relative date formats.
one way is to use the DateTime::modify() function on the object.
$startDate = (new DateTime('now', new DateTimeZone("America/New_York")));
$endDate = (new DateTime('now', new DateTimeZone("America/New_York")));
$startDate->modify("january")
->modify("first day of this month")
->modify("midnight");
$endDate->modify("next year")
->modify("january")
->modify("first day of this month")
->modify("midnight")->modify("-1 second");
var_dump([$startDate, $endDate]);
Try out here: https://www.tehplayground.com/Qk3SkcrCDkNJoLK2
Another way to do this is to separate the relative strings with a comma like so:
$startDate = (new DateTime('first day of january', new DateTimeZone("America/New_York")));
$endDate = (new DateTime('next year, first day of january, -1 second', new DateTimeZone("America/New_York")));
var_dump([$startDate, $endDate]);
Try out here: https://www.tehplayground.com/hyCqXLRBlhJbCyks
Basically date('Y') returns the value of current year. and the first day of each year starts like 2000-01-01.
So this will help you to get exact output.
$firstdate = date( 'Y' ) . '-01-01';
Take a look at this link -- http://davidhancock.co/2013/11/get-the-firstlast-day-of-a-week-month-quarter-or-year-in-php/
function firstDayOf($period, DateTime $date = null)
{
$period = strtolower($period);
$validPeriods = array('year', 'quarter', 'month', 'week');
if ( ! in_array($period, $validPeriods))
throw new InvalidArgumentException('Period must be one of: ' . implode(', ', $validPeriods));
$newDate = ($date === null) ? new DateTime() : clone $date;
switch ($period) {
case 'year':
$newDate->modify('first day of january ' . $newDate->format('Y'));
break;
case 'quarter':
$month = $newDate->format('n') ;
if ($month < 4) {
$newDate->modify('first day of january ' . $newDate->format('Y'));
} elseif ($month > 3 && $month < 7) {
$newDate->modify('first day of april ' . $newDate->format('Y'));
} elseif ($month > 6 && $month < 10) {
$newDate->modify('first day of july ' . $newDate->format('Y'));
} elseif ($month > 9) {
$newDate->modify('first day of october ' . $newDate->format('Y'));
}
break;
case 'month':
$newDate->modify('first day of this month');
break;
case 'week':
$newDate->modify(($newDate->format('w') === '0') ? 'monday last week' : 'monday this week');
break;
}
return $newDate;
}
in PHP 5.3.10 this works
$myDate = new \DateTime(date("Y")."-01-01");
echo $myDate->format("Y-m-d");
In PHP 5.4 and upper you can put all together
echo (new \DateTime(date("Y")."-01-01"))->format("Y-m-d")
As a commenter #Glavić said already
$year = new DateTime('first day of January');
is the solution.
To me it did make sense semantically that "this year" should return midnight of the first day of the year, but indeed it does not!
Following is the code snippet for getting first and last day of the year.
$firstDayOfYear = date('Y-01-01');
$lastDayOfYear = date('Y-12-t');
Try this:
$dt = date('m/d/Y',time());
echo 'First day : '. date("01/01/Y", strtotime($dt)).' - Last day : '. date("m/t/Y", strtotime($dt));
Getting first and last day of the current year using DateTime object.
$currentDate = new \DateTime("NOW");
$yearStartString = $currentDate->format("Y")."-01-01 00:00:00";
$yearEndString = $currentDate->format("Y")."-12-31 23:59:59";
$yearStartDate = \DateTime::createFromFormat("Y-m-d H:i:s", $yearStartString);
$yearEndDate = \DateTime::createFromFormat("Y-m-d H:i:s", $yearEndString);
I have the year, month, week of the month and day of the week. I want to get a date.
For example:
$year = 2019;
$month = 8;
$week = 2; // second week of the month
$day = 2; // tuesday
$date = getDate($year, $month, $week, $day); // $date = '06/08/2019'
You can try to use PHP Relative Formats.
Something like this
$date = new DateTime('first day of August 2019');
$date->modify('tuesday next week');
echo $date->format('Y-m-d');
It needs a little bit of conversion from numbers to strings but it's useful when you need to handle relative dates.
I need to get the next month with php. Everywhere is written these example
date('Y-m-t', strtotime('+1 month'));
The output of the above code is '2017-03-31'. I need to get February, not March.
If you want to get the next irrespective of the current date in the current month. below code may help you
echo date('M',strtotime('first day of +1 month'));
// e.g. "Jan"
echo date('m',strtotime('first day of +1 month'));
// e.g. "1"
echo date('F',strtotime('first day of +1 month'));
// e.g. "January"
This will give you Next month.
You can find more formatting masks in date() function documentation
Use Like This
// One month from today
$date = date('Y-m-d', strtotime('+1 month'));
// One month from a specific date
$date = date('Y-m-d', strtotime('+1 month', strtotime('2015-01-01')));
To get next month using php use string to time function as below:
$nxtm = strtotime("next month");
echo date("M", $nxtm);
date("m", strtotime("2021-08-16 +1 Month"));
You can use the code below to get the next months first day.
$date = (new \DateTime('first day of next month'))->format('Y-m-d');
This is safer than 'next month' or '+1 month' because you may skip some months then.
If you want to have the next month with the same day as this month, or the last day of the next month if it otherwise would switch month you can use this function
function nextMonth()
{
$nextMonthNumber = date('M', strtotime('first day of +1 month'));
$nextMonthDate = new DateTime();
$nextMonthDate->add(new DateInterval('P1M'));
while ($nextMonthDate->format('M') != $nextMonthNumber) {
$nextMonthDate->sub(new DateInterval('P1D'));
}
return $nextMonthDate;
}
I'm trying to get php to calculate a date that was one year and one day ago. I have this:
$date = date(strtotime('-366 days'));
$oneyear_oneday = date("Y-m-d H:i:s", $date);
$date = date(strtotime('-1 year'));
$oneyear = date("Y-m-d H:i:s", $date);
However, due to it being a leap year, both $oneyear and $oneyear_oneday provide the same output. Does anyone know how I can calculate this correctly?
ie if it's 3pm on 15th August 2012, I want the output to be 3pm on the 15th August 2011
with PHP5.3,
$date = new DateTime();
$interval = new DateInterval("P1Y");
$newdate = $date->sub($interval);
First, subtract one year. Then, subtract one day from the result:
$date = strtotime('-1 day', strtotime('-1 year'));
$oneyear_oneday = date("Y-m-d H:i:s", $date);
You can try to use mktime()...
Both calculations are correct. But if you want to get the same date, but one year before, you should simply use '-1 year'. The string '-366 days' is only correct in leap years.
$date = strtotime('2010-01-01 -1 year');
echo date('Y-m-d', $date);
The output stream looks like,
2009-01-01
Go this Link for more reference
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Get first day of week in PHP?
Hi,
I want to find first and last date of current week and last week.
Similarly I want to find first and last date of current month and last month.
This has to be done in PHP. Please help.
strtotime is quite powerful with relative time formats:
strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');
(this only works with PHP 5.3+)
strtotime('first day of this month');
strtotime('last day of this month');
strtotime('first day of last month');
strtotime('last day of last month');
In order to get the first and last date of a month in PHP < 5.3, you can use a combination of mktime and date (date('t') gives the number of days of the month):
mktime(0,0,0,null, 1); // gives first day of current month
mktime(0,0,0,null, date('t')); // gives last day of current month
$lastMonth = strtotime('last month');
mktime(0,0,0,date('n', $lastMonth), 1); // gives first day of last month
mktime(0,0,0,date('n', $lastMonth), date('t', $lastMonth); // gives last day of last month
If you just want to get a string for presentation, then you don't need mktime:
date('Y-m-1'); // first day current month
date('Y-m-t'); // last day current month
date('Y-m-1', strtotime('last month')); // first day last month
date('Y-m-t', strtotime('last month')); // last day last month
Here's a function for the first and last day of the week:
function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y')
{
$wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
$mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
return date($format, $mon_ts);
}
$sStartDate = week_start_date($week_number, $year);
$sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));
It can probably be adapted to do month as well, but I wanted to get my answer in! :)