Check if it is the first monday of a quarter - php

I want to run a script every 1st monday of a month in the windows scheduler. The script itself should check if it is the 1st monday of a quarter (not month).
I know that I can somehow do it with date() or more like the DateTime-object, but I have no idea how to approach this.
An approach I had, even though I don't think that this is very reliable or "clean", is to put the specific months in an array like
// might as well use the numeric month representation...
$quarters = array("January", "April", "July", "October");
and check if it is the first monday of a month and check afterwards, if the current month is in the array quarters. However, feels like this isn't real clean.
If the script runs, it should simply print either:
Today is the 1st monday of a quarter or do nothing. If I would run this job everyday, it should ~90 days do nothing and only once trigger with the given string. If I run it every monday, it should do 12 times nothing and also, only once, trigger.
Hope you can point me in the right direction.

There are a few stages in this solution, I've added comments as they are probably useful in the end code...
$date = new DateTime();
// Calculate start month of quarter from date
$quarterMonth = (floor(($date->format('m') - 1) / 3) * 3) + 1;
// Set date from year, start quarter month and the 1st of the month
$date->setDate($date->format('Y'), $quarterMonth, 1);
// Set result to the first Monday on or after the date
$date->modify('first monday of this month');
echo $date->format('Y-m-d');
running that will give (as of date of answer)...
2021-01-04

I would suggest building a relative date string such as first monday of january 2024 to construct the date:
$s1 = "2024-03-31";
$d1 = new DateTime($s1);
// the floor(...) expression below maps 1/2/3 to 0, 4/5/6 to 1 and so on
$yy = $d1->format("Y");
$mm = ["january", "april", "july", "october"][floor(($d1->format("n") - 1) / 3)];
$d2 = new DateTime("first monday of $mm $yy");
echo $d2->format("D Y-m-d H:i:s");
// result:
// Mon 2024-01-01 00:00:00

Old school solution with date() and strtotime():
function isFirstMondayOfQuarter($date = 'today'){
$ts = strtotime($date);
if(date('n',$ts)%3 != 1) return false;
return $ts === strtotime('first monday of this month',$ts);
}
var_dump(isFirstMondayOfQuarter()); //bool(false) at 10 Feb 2021
var_dump(isFirstMondayOfQuarter('2021-01-04')); // bool(true)

Related

Get previous month value in PHP

I have used the below PHP function to get the previous month,
$currmonth = date('m', strtotime('-1 month'));
It was working fine and I was getting the value of 04 till yesterday. On today May 31st (Last day of the month May), I noticed the function returns the current month only. That is 05. Is there any other alternate function which returns the previous month accurately.
Try strtotime("first day of last month").
The first day of is the important part as detailed here.
Literally ask strtotime for the 'first day of the previous month' this makes sure it selects the correct month:-
$currmonth = date("m", strtotime("first day of previous month"));
You can use OOP with DateTime class and modify method:
$now = new DateTime();
$previousMonth = $now->modify('first day of previous month');
echo $previousMonth->format('m');
strtotime() works accurately. The problem is what you ask it to return.
"-1 month" is not the same as "previous month". It is the same as "subtract 1 from current month then normalize the result".
On 2017-05-31, subtracting 1 from current month gets 2017-04-31 which is not a valid date. After normalization, it becomes 2017-05-01, hence the result you get.
There are more than one way to get the value you need. For example:
// Today
$now = new DateTime('now');
// Create a date interval string to go back to the first day of the previous month
$int = sprintf('P1M%dD', $now->format('j')-1);
// Get the first day of the previous month as DateTime
$fdopm = $now->sub(new DateInterval($int));
// Verify it works
echo($fdopm->format('Y-m-d'));
// On 2017-05-31 it should print:
// 2017-04-01
If you just need to get the month number of previous month, the following should suffice.
$m = idate("m") - 1;
// wrap to previous year
if ($m < 1) {
$m = 12 - abs($m) % 12;
}
This works with arbitrary number of subtracted months.

PHP: get next date based on fixed base date

is there a way in PHP to get the next date(s) using a 4-week interval from a given date ?
Example:
My start date is Friday, Jan 03, 2014 and my interval is every 4 weeks from that date.
What I am looking for is the next date (or dates, if possible) from the current date that matches this 4-week interval.
In the above example this would be Friday, May 23, 2014 (then June 20, 2014, July 18, 2014 etc.).
I know I can get the current date as follows: $today = date('Y-m-d');
and I could probably set the start date like this: $start = date('2014-01-03');
but I don't know how to calculate the interval and how to find out the next matching date(s).
You should read up on the DateTime classes, specifically DatePeriod and DateInterval:
$start = new DateTime('2014-01-03');
$interval = DateInterval::createFromDateString('4 weeks');
$end = new DateTime('2015-12-31');
$occurrences = new DatePeriod($start, $interval, $end);
foreach ($occurrences as $occurrence) {
echo $occurrence->format('Y-m-d') . PHP_EOL;
}
DatePeriod takes a start date and a DateInterval and allows you traverse over the object to get all dates within the boundaries using the given interval. The cut off can be either a set number of cycles (so the next 10 dates) or an end date (like above), even if the end date is not one of the dates the interval falls on (it will stop below it). Or you can use an 8601 interval notation string (which sounds so much fun, huh?), but I'm pretty shaky on that.
If 4-week interval means 7 x 4 = 28 days, you can obtain the "next date" by:
$today = new DateTime();
$next_date = $today->add(new DateInterval('P28D'));
$next_next_date = $next_date->add(new DateInterval('P28D'));
$next_next_next_date = $next_next_date->add(new DateInterval('P28D'));
And if you want to calculate more "next dates", you can repeat the add() to repetitively add 28 days to your date.
Note: Beside using P28D, you can use P4W, which means 4 weeks.
While some answers may suggest using strtotime(), I find the object-oriented approach more structured. However, DateInterval is only available after PHP >= 5.3.0 (while DateTime is available after PHP >= 5.2.0)
You could use strtotime()
echo date('Y-m-d', strtotime('now +4 weeks'));
UPDATED:
$start = date('Y-m-d', strtotime('2014-01-03 +4 weeks'));
echo $start;
You could also run this in a for loop to get the next 6 or more dates. For example:
$Date = "2014-01-03";
$Int = 6;
for($i=0; $i<$Int; $i++){
$Date = date('Y-m-d', strtotime('{$Date} +4 weeks'));
echo $Date;
}

Display the 1st, 2nd, 3rd, 4th, & 5th date of the current week in php

Ok, So im trying to figure out the best way in php to write (or print) the 1st, 2nd, 3rd, 4th, & 5th date of the current week (starting with Monday) in php.
For example... Using the week of the 4th through the 8th of February...
I would need a script for the 1st day of the week (starting with Monday) displayed as February 04, 2013 and then I would need the same script four more times to display Tues, Wed, Thurs, & Fri...
All-together I would end up with 5 scripts or one script that I could copy and manipulate to work the way I need it to...
Also, If you could tell me how to do the same for Saturday and Sunday that would be greatly appreciated as-well.
If there is anything that you do not understand, please let me know and I will try my hardest to clarify...
Thanks in advance!
First you need to get the "current" week's Monday. To do this, I would suggest calling date("N") and see if it's 1. If it is, then now is the Monday you want. Otherwise last monday is. Pass that to strtotime to get the timestamp corresponding to the first monday of the week. Then repeatedly add 24 hours (24*3600 seconds) to get each day.
$startofweek = date("N") == 1 ? time() : strtotime("last monday");
for($i=0; $i<5; $i++) {
$day = $startofweek + 24*3600*$i;
echo "Day ".($i+1).": ".date("d/M/Y",$day)."<br />";
}
You can use strtotime with special parameters. like:
$time = strtotime('monday this week');
$time = strtotime('today');
$time = strtotime('next monday');
$time = strtotime('previous monday');
Same goes for every other days of the week

php week interval from date ("W")

Hy, I have in database number of the week date ("W") and I want to display week interval like 28 Jan -> 3 Feb in this format, and I don't know if it's possible. Can you help?
Thanks!
Try this
$year = 2013;
$week_no = 6;
$week_start = new DateTime();
$week_start->setISODate($year,$week_no);
$week_end = clone $week_start;
$week_end = $week_end->add(new DateInterval("P1W"));
echo $week_start->format('d-M-Y') . " - ".$week_end->format('d-M-Y');
Transform your intervals into timestamps.
If its not the first day of the week get the first day of that week with strtotime "last sunday" (or monday) for the first date.
Do the same for the second date this time geting the last day of the week with "next saturday" (or sunday);
Get both dates W and make a mysql comparison between the weeks.

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

I need to get previous month and year, relative to current date.
However, see following example.
// Today is 2011-03-30
echo date('Y-m-d', strtotime('last month'));
// Output:
2011-03-02
This behavior is understandable (to a certain point), due to different number of days in february and march, and code in example above is what I need, but works only 100% correctly for between 1st and 28th of each month.
So, how to get last month AND year (think of date("Y-m")) in the most elegant manner as possible, which works for every day of the year? Optimal solution will be based on strtotime argument parsing.
Update. To clarify requirements a bit.
I have a piece of code that gets some statistics of last couple of months, but I first show stats from last month, and then load other months when needed. That's intended purpose. So, during THIS month, I want to find out which month-year should I pull in order to load PREVIOUS month stats.
I also have a code that is timezone-aware (not really important right now), and that accepts strtotime-compatible string as input (to initialize internal date), and then allows date/time to be adjusted, also using strtotime-compatible strings.
I know it can be done with few conditionals and basic math, but that's really messy, compared to this, for example (if it worked correctly, of course):
echo tz::date('last month')->format('Y-d')
So, I ONLY need previous month and year, in a strtotime-compatible fashion.
Answer (thanks, #dnagirl):
// Today is 2011-03-30
echo date('Y-m-d', strtotime('first day of last month')); // Output: 2011-02-01
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
if the day itself doesn't matter do this:
echo date('Y-m-d', strtotime(date('Y-m')." -1 month"));
I found an answer as I had the same issue today which is a 31st. It's not a bug in php as some would suggest, but is the expected functionality (in some since). According to this post what strtotime actually does is set the month back by one and does not modify the number of days. So in the event of today, May 31st, it's looking for April-31st which is an invalid date. So it then takes April 30 an then adds 1 day past it and yields May 1st.
In your example 2011-03-30, it would go back one month to February 30th, which is invalid since February only has 28 days. It then takes difference of those days (30-28 = 2) and then moves two days past February 28th which is March 2nd.
As others have pointed out, the best way to get "last month" is to add in either "first day of" or "last day of" using either strtotime or the DateTime object:
// Today being 2012-05-31
//All the following return 2012-04-30
echo date('Y-m-d', strtotime("last day of -1 month"));
echo date('Y-m-d', strtotime("last day of last month"));
echo date_create("last day of -1 month")->format('Y-m-d');
// All the following return 2012-04-01
echo date('Y-m-d', strtotime("first day of -1 month"));
echo date('Y-m-d', strtotime("first day of last month"));
echo date_create("first day of -1 month")->format('Y-m-d');
So using these it's possible to create a date range if your making a query etc.
If you want the previous year and month relative to a specific date and have DateTime available then you can do this:
$d = new \DateTimeImmutable('2013-01-01', new \DateTimeZone('UTC'));
$firstDay = $d->modify('first day of previous month');
$year = $firstDay->format('Y'); //2012
$month = $firstDay->format('m'); //12
date('Y-m', strtotime('first day of last month'));
strtotime have second timestamp parameter that make the first parameter relative to second parameter. So you can do this:
date('Y-m', strtotime('-1 month', time()))
if i understand the question correctly you just want last month and the year it is in:
<?php
$month = date('m');
$year = date('Y');
$last_month = $month-1%12;
echo ($last_month==0?($year-1):$year)."-".($last_month==0?'12':$last_month);
?>
Here is the example: http://codepad.org/c99nVKG8
ehh, its not a bug as one person mentioned. that is the expected behavior as the number of days in a month is often different. The easiest way to get the previous month using strtotime would probably be to use -1 month from the first of this month.
$date_string = date('Y-m', strtotime('-1 month', strtotime(date('Y-m-01'))));
I think you've found a bug in the strtotime function. Whenever I have to work around this, I always find myself doing math on the month/year values. Try something like this:
$LastMonth = (date('n') - 1) % 12;
$Year = date('Y') - !$LastMonth;
date("m-Y", strtotime("-1 months"));
would solve this
Perhaps slightly more long winded than you want, but i've used more code than maybe nescessary in order for it to be more readable.
That said, it comes out with the same result as you are getting - what is it you want/expect it to come out with?
//Today is whenever I want it to be.
$today = mktime(0,0,0,3,31,2011);
$hour = date("H",$today);
$minute = date("i",$today);
$second = date("s",$today);
$month = date("m",$today);
$day = date("d",$today);
$year = date("Y",$today);
echo "Today: ".date('Y-m-d', $today)."<br/>";
echo "Recalulated: ".date("Y-m-d",mktime($hour,$minute,$second,$month-1,$day,$year));
If you just want the month and year, then just set the day to be '01' rather than taking 'todays' day:
$day = 1;
That should give you what you need. You can just set the hour, minute and second to zero as well as you aren't interested in using those.
date("Y-m",mktime(0,0,0,$month-1,1,$year);
Cuts it down quite a bit ;-)
This is because the previous month has less days than the current month. I've fixed this by first checking if the previous month has less days that the current and changing the calculation based on it.
If it has less days get the last day of -1 month else get the current day -1 month:
if (date('d') > date('d', strtotime('last day of -1 month')))
{
$first_end = date('Y-m-d', strtotime('last day of -1 month'));
}
else
{
$first_end = date('Y-m-d', strtotime('-1 month'));
}
If a DateTime solution is acceptable this snippet returns the year of last month and month of last month avoiding the possible trap when you run this in January.
function fn_LastMonthYearNumber()
{
$now = new DateTime();
$lastMonth = $now->sub(new DateInterval('P1M'));
$lm= $lastMonth->format('m');
$ly= $lastMonth->format('Y');
return array($lm,$ly);
}
//return timestamp, use to format month, year as per requirement
function getMonthYear($beforeMonth = '') {
if($beforeMonth !="" && $beforeMonth >= 1) {
$date = date('Y')."-".date('m')."-15";
$timestamp_before = strtotime( $date . ' -'.$beforeMonth.' month' );
return $timestamp_before;
} else {
$time= time();
return $time;
}
}
//call function
$month_year = date("Y-m",getMonthYear(1));// last month before current month
$month_year = date("Y-m",getMonthYear(2)); // second last month before current month
function getOnemonthBefore($date){
$day = intval(date("t", strtotime("$date")));//get the last day of the month
$month_date = date("y-m-d",strtotime("$date -$day days"));//get the day 1 month before
return $month_date;
}
The resulting date is dependent to the number of days the input month is consist of. If input month is february (28 days), 28 days before february 5 is january 8. If input is may 17, 31 days before is april 16. Likewise, if input is may 31, resulting date will be april 30.
NOTE: the input takes complete date ('y-m-d') and outputs ('y-m-d') you can modify this code to suit your needs.

Categories