I have five files, namely index.php and first.php .. fourth.php.
first.php through fourth.php represent an annual calendar divided into 4 quarters (3 months per file). Now, I just need to display within the index page the current quarter. For example, since it's August, my index should display third.php, which contains July, August and September. In short, I need to make my index page dynamic.
This is my current code, which is obviously static:
if(!isset($_GET['quarter']) && !isset($_GET['year'])){
include ('/thirdq2012.php');
}
Also, how would I test this?
EDIT: problem fixed! i have another question, on top of every page, i have a drop down year button, which switches from 2011, 2012, 2013. my next problem is how to display the specific quarter for the selected year without creating multiple files? example quarter 1(january, february, march) of year 2011 contains data and other values that the user input, but quarter 1 of year 2012 doesnt contain anything. how will i switch on from there?
<?php
$now = new DateTime();
$month = (int)$now->format("m");
if ($month >= 1 AND $month <= 3) {
echo "First Quarter!";
}
elseif ($month >= 4 AND $month <= 6) {
echo "Second Quarter!";
}
elseif ($month >= 7 AND $month <= 9) {
echo "Third Quarter!";
}
else {
echo "Fourth Quarter!";
}
Related
I have job_no with other fields in my database, I need to identify the date and according to that date job no should be generated automatically. The date criteria is financial year April to March.
If financial year change, job no should start from 1 else continue.
What can I do in this situation?
I'm using php and mysql.
Thanks.
Make a function in php that check for the date and month so that you get to know which financial year a date lies. In that function you can check the financial year of current date and according to that either continue with the last incremented value, or restart the value from 1.
The function definition is something like:
if (date('m') > 3) {
$year = date('Y') +1;
}
else {
$year = $date('Y');
}
or
if (date('m') > 3) {
$year = date('Y')."-".(date('Y') +1);
}
else {
$year = (date('Y')-1)."-".date('Y');
}
I am kind of stuck on figuring out how to do this.
In my array that I am looping over, it has multiple arrays with the following fields year and quarter. Year is in numeric form like 2012 and quarter is in text form like Q1 or Q2
Now I am given a range for example
from: Q1 2013 to: Q3 2014
I have access to the numeric form of the quarters in the range.
So this is the variables I have from the example above: $quarter_from, $quarter_to, $year_from, and $year_to
I need to figure out if the current array in the loop that has the fields year and quarter is in the range of the range given.
How do I go about checking? Sure I can do a simple numeric check for years but quarters is a whole another story. For example the current array could have Q4 and 2013 and that's obviously in the range of the example, and the first case would pass by doing a year check. But for quarter, how do I check that?
I think you can do a concatenate of the quarter and year like this, assuming that the range that is selected logically makes sense (to comes later than from with respect to time).
from: 20122 => year 2012 and quarter 2
to: 20141 => year 2014 and quarter 1
So even though your quarter's value is in to is less than the quarter's value in from you still get a higher value in to because of the year.
This way you can now do a range check like this, by concatenating your year and quarter fields (do a substr() to get the second character of the string and convert to number then concatenate).
For example if 2013, Q4 shows up.
That looks like 20134 then do a range check like this
20122 <= 20134 <= 20141 which should work.
Wrote this in a hurry, it's more of a phsudo code but should give you some idea:
From: Q2 2013
Middle: Q1 2014 (checking if in range of From and To)
To: Q1 2014
Now we need to compare the years of from and middle and middle and to, and if the year is the same in either case you can split the quarter to get the number of the quarter and compare the quarters to see if the middle part is actually in the range.
fromQuarter = from.quarter.split("")[1] // Q2 => 2
middleQuarter = middle.quarter.split("")[1] // Q1 => 1
toQuarter = to.quarter.split("")[1] // Q1 => 1
If to.year >= middle.year && middle.year >= from.year
// Year range legit.
if(to.year == middle.year || from.year == middle.year)
// We need to check the quarters.
if(to.year == middle.year)
// Need to compare the quarter of the to year and the middle year.
if(toQuarter >= middleQuarter )
// In range.
else
// Out of range.
if(from.year == middle.year)
// Need to compare the quarter of the from year and the middle year.
if(fromQuarter <= middleQuarter)
// In range.
else
// Out of range.
else
// Between two distinct years, in range.
else
// Not in range.
I'll check this again soon to see if I made an logic error.
Try this:
function expand($quarter, $year) {
return $year * 4 + $quarter;
}
$quarter_from = 1; $year_from = 2013;
$quarter_to = 3; $year_to = 2014;
$quarter = 3; $year = 2014;
$n1 = expand($quarter_from, $year_from);
$n2 = expand($quarter_to, $year_to);
$n = expand($quarter, $year);
$is_in_range = $n >= $n1 && $n <= $n2;
So I'm making a website for this pizzeria.
In the past I made a simple window that pops up Monday-Friday & Between 11.30am-02:00pm which is simple enough.
But now I've had a request to do the same thing, but instead of having a single "Lunch Buffe Ongoing!" box, the client want's a daily menu (different menu every day, which is also easy enough), but he has 3 different daily menus alternating every week.
So here's an example:
Week 1 Menu:
Monday: w1m1
Tuesday: w1m2
..
Week 2 Menu:
Monday: w2m1
Tuesday: w2m2
So my question is:
Is there a PHP function / equation for calculating "is it the first, second or third" week from a specified starting date
Edit (Not duplicate because):
This isn't a duplicate of PHP get number of week for month because I'm not asking for the week number, I'm asking what week this is alternating with 3
Answer:
<?php
function menuNumber($now) {
$then = '26/05/2014';
$first = DateTime::createFromFormat('d/m/Y', $then);
$second = DateTime::createFromFormat('d/m/Y', $now);
$calculated = (floor($first->diff($second)->days/7) % 3) + 1;
return $calculated;
}
echo "Menu Number " . menuNumber(date("d/m/Y")); // Menu Number 1
?>
Help From:
php weeks between 2 dates
PHP
<?php
function menuNumber($now) {
$then = '26/05/2014';
$first = DateTime::createFromFormat('d/m/Y', $then);
$second = DateTime::createFromFormat('d/m/Y', $now);
$calculated = (floor($first->diff($second)->days/7) % 3) + 1;
return $calculated;
}
echo "Menu Number " . menuNumber(date("d/m/Y")); // Menu Number 1
?>
Help From: PHP weeks between 2 dates
I am trying to come up with the most efficient and best way to accomplish this somewhat of a complex situation. I know that I could build this solution using probably around 5 if else statements, maybe more - however there must be a better way to accomplish what I want to.
So here's what I am trying to do. I have an events page on my website, and what I want to do is display the dates in a minimalistic way when possible. What I mean is the following:
Say I have 3 dates: May 5, May 6, May 7. I want to display it as: May 5 - 7.
However, there will be situations where the dates may be: May 5, May 7. In this case I would like to display it as: May 5 & 7.
However, there may also be situations where the dates may be: May 25, June 2. In this case I would like to display it as: May 25 & June 2.
However! There also may be situations where the dates may be: May 25, May 26, June 2. In this case it should display as: May 25 - 26 & June 2
Of course, there could just be a single date as well. But one other thing, it could be possible that there could be more than 3 dates as well, so it would be nice if it could work regardless of how many dates there are (IE loop through an array).
I know that we are suppose to make an attempt and show some code to debug, however I don't even know where to start with this, if this is too much for someone to put together - just giving me an idea of how to do something like this efficiently would be a huge help.
Thanks
//input data: sorted list of dates
$dates = array('May 5','May 6','May 7','May 30','Jun 2','Jun 3','Dec 11','Dec 12','Dec 14');
array_push($dates,false); //add an extra value so the last range gets printed
//initialize min & previous date as first date
$min_date = array_shift($dates);
$prev_date = $min_date;
$counter = 0; //keep count of # of days between min and max
$formatted_dates = array();
foreach($dates as $date) {
//if the difference in number of days between current date and min date
//is greater than counted number of days then we capture the current range
//and start a new one by resetting $min_date to $date and $counter to 0
if(!$date || ($counter + 1) < diff_in_days($min_date,$date)) {
if($counter == 0) { //format for 1 date
$formatted_dates[] = $min_date;
}
elseif($counter == 1) { //format for 2 dates
$formatted_dates[] = "$min_date & $prev_date";
}
elseif($counter > 1) { //format for > 2 dates
$formatted_dates[] = "$min_date - $prev_date";
}
$counter = 0;
$min_date = $date;
}
else {
$counter++;
}
$prev_date = $date;
}
//may also want to verify that neither formatted date contains an '&'
//so you don't end up with "May 11 & May 12 & June 1 & June 2" which may be confusing
if(count($formatted_dates) == 2) {
print implode(' & ',$formatted_dates);
}
else {
print implode("\n",$formatted_dates);
}
function diff_in_days($day1,$day2) {
$datetime1 = new DateTime($day1);
$datetime2 = new DateTime($day2);
$interval = $datetime1->diff($datetime2);
$ret = (int) $interval->format('%a');
return $ret;
}
Output
May 5 - May 7
May 30
Jun 2 & Jun 3
Dec 11 & Dec 12
Dec 14
I have debugged this legacy code, and would like a sanity check on it.
The purpose of it is to allow someone to choose a delivery frequency for shipping a product. If someone wants their product Every Other Week, the system needs to determine if they should get an order next week, or two weeks from now. We call it A week, or B Week.
Keep in mind I did not write this, I am just trying to make sense of it and would like some help evaluating its accuracy:
if (date("l") == "Monday" ) {
$start = 0;
} else if (date("l") == "Tuesday" || date("l") == "Wednesday" || date("l") == "Thursday" || date("l") == "Friday" || date("l") == "Saturday"|| date("l") == "Sunday") {
$start = -1;
}
// if changing to every other week set to next week's a/b-ness
$a_week_tid = 34;
$b_week_tid = 35;
$every_other_week_frequency_id = 32;
if ($delivery_frequency == $every_other_week_frequency_id) {
$julian = (int) (strtotime('Monday +' . $start . ' week') / 86400);
$julian_week = ($julian-4) / 7;
if ($julian_week % 2) {
$today_a_or_b = $b_week_tid;
$next_week_a_or_b = $a_week_tid;
$a_or_b_week_string = '(A Week)';
} else {
$today_a_or_b = $a_week_tid;
$next_week_a_or_b = $b_week_tid;
$a_or_b_week_string = '(B Week)';
}
} else {
$next_week_a_or_b = NULL;
$a_or_b_week_string = NULL;
}
This code is not commented or documented. The part that confuses me is:
Why is 4 subtracted from Julian, then divided by 7?
If today is Monday, $julian_week is 2129, and 2129 % 2 evaluates TRUE. Is that correct?
Is this even how it should be done? Can't I rewrite this using date('w') a lot easier?
Yeah using date would totally be easier, plus it takes into account leap years, daylight saving time, all that extra stuff you don't want to have to deal with.
if (date('W')%2==1)
That's SOOOO much easier to maintain than the above.
I don't believe you can use date("W") in this case. According to the ISO calculation, on occasion, there will be years with 53 weeks. In those years, Week 53 is followed by Week 01, both odd numbers, and an A/B calculation based on Even/Odd ISO week number would result in two successive A or B weeks.
The original calculation determines the number of days from the UNIX epoch of the present Monday, or of the most recent Monday if today is not a Monday. The -4 causes the A/B week labels to change on Thursdays. Even/oddness of a week is determined from a fixed date (the Unix Epoch), so there will be no discontinuity in the oscillation of A/B-ness using the original code.
The ISO standard for week one in a year is that it is the week that the first Thursday of the year falls. This is the reason for the 4 subtracted from the Julian date. The week number is then found by dividing by 7.
Again the ISO standard implies that week number cannot be greater than 53. I don't understand how your figure of 2129 can arise. However the div operator will not evaluate TRUE for this figure. Checking the div operator on the week number is the way of determining whether you are in week a or b. If it is before Thursday, it is quite likely that the number will be 1 less than you anticipate.
The coding looks fairly good to me, though I have not stepped through all of it. It does look correct.
Using W on consecutive Fridays, mod by 2. Both lines output 1. So doing it this way will occasionally fail.
echo date('W',strtotime('2016-01-01'))%2;
echo date('W',strtotime('2016-01-08'))%2;
Just a simple way.
<?php
$weekNumber = date("W");
echo 'Week number:',$weekNumber;
if($weekNumber&1) {
echo '<strong>Week A.</strong>';
} else {
echo '<strong>Week B.</strong>';
}
?>
$day = '2019-11-10';
$date = new DateTime($day);
$dayOfMonth = $date->format("j"); // month days 1 - 30
$weekNumber = ceil($dayOfMonth / 7); // get the week number
if ($weekNumber % 2 == 0) { //if week number is even
echo "Even Week";
} else {
echo "Odd Week";
}
**// output Even Week**