for ($y = 25; $y >= 7; $y--)
{
$showYear = false;
for ($m = 12; $m >= 1; $m--)
{
if (blogList($m, $y))
$showYear = true;
}
if ($showYear) {
echo '<h2>' . (2000 + $y) . '</h2>';
for ($m = 12; $m >= 1; $m--)
{
echo blogList($m, $y);
}
}
}
//blog archives
function blogList($month, $year)
{
$lastDate = array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
$beginDate = mkTime(0, 0, 0, $month, 1, $year);
$endDate = mkTime(0, 0, 0, $month, $lastDate[$month - 1], $year);
$query = .......;
}
i don't know why he set the $y=25.$showYear = false;
why the $lastDate = array(31,29,31,30,31,30,31,31,30,31,30,31);?
$y = 25 is because he's looping backwards from 2025 to 2007. $y ends up as the year argument for mkTime (see http://php.net/manual/en/function.mktime.php).
that array holds the last date of each calendar month, e.g. January has 31 days.
To show blog posts from 2007 to 2025.
The number of days in the month of each month.
However, don't try to learn from the above code!
Because blogList($m,$y) expects values for $y in the range of 7-25.
Those are the last dates of the months on a Gregorian calendar.
$lastdate holds the number of days in each of the 12 months of the year, and before running the query its trying to find the beginning and ending days of the month.
I'm not sure about the y=25 - that might depend on whats defined in the $query variable
1) Because it's the date from 2007 to 2025. The variable it's set to false because it needs to check something inside blogList, it's counting the days remaining to the end of the month, I think.
2) That's the last day of the month. The array has 12 elements.
Related
I've been using the following function fine until the other day when the clocks went forward:
function months($month_format="F") {
$array = array();
for ($i = 1; $i <=12; $i++) {
$array[$i]['string'] = date($month_format, mktime(0,0,0,$i));
$array[$i]['int'] = date('m', mktime(0,0,0,$i));
}
return $array;
}
It outputs an array with:
string[]
int[]
Since the other day (like 2 days ago, when the clocks went forward in the UK), the function seems to be showing me 2 march months and 0 Februarys....
Very very strange...
I'm using rethinkdb with their eachPosTime function... not sure if this is causing it.
I tried using a different function, but still using mktime:
function months(){
$start_month = 1;
$end_month = 12;
$start_year = date("Y");
$array = array();
for($m=$start_month; $m<=12; ++$m){
echo $m.'<br>';
$array[$m]['string'] = date('F', mktime(0,0,0,$m));
$array[$m]['int'] = date('m', mktime(0,0,0,$m));
if($start_month == 12 && $m==12 && $end_month < 12)
{
$m = 0;
$start_year = $start_year+1;
}
//echo date('F Y', mktime(0, 0, 0, $m, 1, $start_year)).'<br>';
if($m == $end_month) break;
}
return $array;
}
Still, I am having no luck.
Check the image here, which shows the output of the months() function:
Output of the months() function
This is not to do with the clocks changing, but the time of the month, and the entirely unhelpful signature of the mktime function.
When you leave out parameters from a "mktime" call, they are filled in with today's date. In your case, you specified month without day or year, so when you ask for mktime(0, 0, 0, 2); it will fill in today's day and year, and look for the 29th February 2021 - a day that doesn't exist. Since the time library "overflows" dates, this becomes the 1st March.
The solution is to pass an explicit day to "mktime", as in mktime(0,0,0,$m,1) or to use a less confusing function.
Basically, I have a date set to today. From that date, I want to get the next and previous 20th day of the month. Basically, starting with 07/10/2020 (date of the post in d/m/Y format), I should end up with a date/timestamp to 20/09/2020 (previous 20th) and 20/10/2020 (next 20th).
I tried a few things and ended up with:
$ld_next_str = date("d/m/Y", strtotime("20 day this month"));
$ld_next_dt = DateTime::createFromFormat("d/m/Y", $ld_next_str);
$ld_prev_str = date("d/m/Y", strtotime("20 day last month"));
$ld_prev_dt = DateTime::createFromFormat("d/m/Y", $ld_prev_str);
But turns out this : "20 day this month" just adds 20 days to the current day so right now (on the seventh) it returns the 27th.
Knowing this, I figured I could probably just do something like subtracting the current day to that date (27 - 7 = 20) but I feel like there's probably a simpler way to do it, using strtotime without any extra steps.
Thanks in advance!
PS : I know you can get some days using "first" / "second" / "third" etc... but (I think) it doesn't go any higher than "twelfth", because I tried "twentieth" and that didn't work...
The formula for the previous 20th is:
mktime(0, 0, 0, $month - (1 - floor($day / 20)), 20, $year))
The formula for the next 20th is:
mktime(0, 0, 0, $month + floor($day / 20), 20, $year))
As a demo:
for ($month = 1; $month <= 12; $month++) {
for ($day = 1; $day <= 30; $day += 5) {
$prev = mktime(0, 0, 0, $month - (1 - floor($day / 20)), 20, 2020);
$next = mktime(0, 0, 0, $month + floor($day / 20), 20, 2020);
echo "$day/$month/2020: ", date('d/m/Y', $prev), ', ', date('d/m/Y', $next), PHP_EOL;
}
}
Outputs:
1/1/2020: 20/12/2019, 20/01/2020
6/1/2020: 20/12/2019, 20/01/2020
11/1/2020: 20/12/2019, 20/01/2020
16/1/2020: 20/12/2019, 20/01/2020
21/1/2020: 20/01/2020, 20/02/2020
26/1/2020: 20/01/2020, 20/02/2020
1/2/2020: 20/01/2020, 20/02/2020
6/2/2020: 20/01/2020, 20/02/2020
11/2/2020: 20/01/2020, 20/02/2020
16/2/2020: 20/01/2020, 20/02/2020
21/2/2020: 20/02/2020, 20/03/2020
...
Solution with an external class (a DateTime extension) that supports the cron syntax.
The string "0 0 20 * *" specifies the 20th of every month and every weekday at 00:00.
The nextCron() and previousCron() methods are used to determine the previous/next point in time based on a specific date.
$cronStr = "0 0 20 * *"; //every 20th 00:00
//source on https://github.com/jspit-de/dt
$next20 = dt::create('now')->nextCron($cronStr);
echo $next20; //2020-10-20 00:00:00
$prev20 = dt::create('now')->previousCron($cronStr);
echo $prev20; //2020-09-20 00:00:00
Outputs for the execution on October 8, 2020.
Here a better solution by using DateTime
$lastMonth = (new DateTime("last month"))->format("Y-m-20");
$thisMonth = (new DateTime("this month"))->format("Y-m-20");
$nextMonth = (new DateTime("next month"))->format("Y-m-20");
echo "$lastMonth - $thisMonth - $nextMonth";
Why do I get two March instead of February then March when running the following code?
<?php
for($i=1; $i<= 12; $i++){
//MONTH NAME
$monthName = strftime('%B', mktime(0, 0, 0, $i));
if($i <= 9){
$monthNr = "0".$i;
}
else{
$monthNr = $i;
}
echo $monthNr . ':' . ucfirst($monthName) ."\n";
}
?>
Output:
01:January
02:March
03:March
04:April
05:May
06:June
07:July
08:August
09:September
10:October
11:November
12:December
It's because of this:
(from the mktime function reference)
Arguments may be left out in order from right to left; any arguments thus omitted will be set to the current value according to the local date and time.
If the current day is greater than the last day of the specified month, the extra days will extend the resulting timestamp into the next month. In this case today is the 30th, so February 30th becomes March 2nd.
If you add a day to mktime you'll get the result you're expecting.
$monthName = strftime('%B', mktime(0, 0, 0, $i, 1));
I'm trying to do a simple loop of the days in the month of that year. So I'm using a for loop in PHP. I assigned a date April 2013, $date = mktime(0, 0, 0, 4, 1, 2013). I also determined the numbers of days in that month, $days = date("t", $date). And then I did the loop using for loop so that for ($i = 1; $i <= $d1; $i++) it'll display all the days in the month of April 2013, which is 30 days (output: 1, 2, 3, 4, 5....30). I'd like to display this in HTML so that it'll look like a calendar month of April 2013 (which I can figure out later). However, I think I have this "writer's block" per se (or programming block so on). I want to display the so that when $i = 4, displays separately with a red color indicating that it is a special day. This is my main thing I'm stuck with. My code is as follows:
$date = mktime(0, 0, 0, 4, 1, 2013);
$days = date("t", $date);
for ($i = 1; $i <= $days; $i++) { echo $i; } // except when $i = 4, echoes it as red/bold
Any ideas? Thanks in advance.
$date = mktime(0, 0, 0, 4, 1, 2013);
$days = date("t", $date);
for ($i = 1; $i <= $days; $i++) {
if(($i % 4))
{
echo '<span style="color: #f00; font-weight: bold;">'.$i.'</span>';
} else { echo $i; }
}
Untested, but should be what you're after...
$date = mktime(0, 0, 0, 4, 1, 2013);
$days = date("t", $date);
for ($i = 1; $i <= $days; $i++) {
if($i % 4 == 0){
echo "<div style='color: red'>" . $i . "</div>";
}
else{
echo $i;
}
}
PHP provides ways to get the number of the current day of the month (date('j')) as well as the number of the current day of the year (date('z')). Is there a way to get the number of the current day of the current quarter?
So right now, August 5, it is day 36 of the third quarter.
If there is no standard way of calculating this, does anyone have a (prefereably PHP-based) algorithm handy?
How about:
$curMonth = date("m", time());
$curQuarter = ceil($curMonth/3);
I wrote a class with the following methods. Enjoy.
public static function getQuarterByMonth($monthNumber) {
return floor(($monthNumber - 1) / 3) + 1;
}
public static function getQuarterDay($monthNumber, $dayNumber, $yearNumber) {
$quarterDayNumber = 0;
$dayCountByMonth = array();
$startMonthNumber = ((self::getQuarterByMonth($monthNumber) - 1) * 3) + 1;
// Calculate the number of days in each month.
for ($i=1; $i<=12; $i++) {
$dayCountByMonth[$i] = date("t", strtotime($yearNumber . "-" . $i . "-01"));
}
for ($i=$startMonthNumber; $i<=$monthNumber-1; $i++) {
$quarterDayNumber += $dayCountByMonth[$i];
}
$quarterDayNumber += $dayNumber;
return $quarterDayNumber;
}
public static function getCurrentQuarterDay() {
return self::getQuarterDay(date('n'), date('j'), date('Y'));
}
function date_quarter()
{
return ceil(date('n', time()) / 3);
}
or
function date_quarter()
{
$month = date('n');
if ($month <= 3) return 1;
if ($month <= 6) return 2;
if ($month <= 9) return 3;
return 4;
}
You can use Carbon it has easy modifiers for getFirstOf{Month,Year,Quarter}()
<?php
//take current date
$now = Carbon\Carbon::now();
//modify a copy of it to the first day of the current quarter
$firstOfQuarter = $now->copy()->firstOfQuarter();
//calculate the difference in days and add 1 to correct the index
$dayOfQuarter = $now->diffInDays($firstOfQuarter) + 1;
Assuming you mean a calendar-quarter (because a company fiscal year can start in any month of the year), you could rely on the date('z') to determine the day-of-year, and then keep a simple array of the day each quarter starts on:
$quarterStartDays = array( 1 /* Jan 1 */, 90 /* Mar 1, non leap-year */, ... );
Then with the current day-of-year you can first locate the largest start-day that's less than or equal to the day-of-year, then subtract.
Note that you need different numbers depending on the leap year.
<?php
function day_of_quarter($ts=null) {
if( is_null($ts) ) $ts=time();
$d=date('d', $ts);
$m=date('m', $ts)-1;
while($m%3!=0) {
$lastmonth=mktime(0, 0, 0, $m, date("d", $ts), date("Y",$ts));
$d += date('t', $lastmonth);
$m--;
}
return $d;
}
echo day_of_quarter(mktime(0, 0, 0, 1, 1,2009));
echo "\n";
echo day_of_quarter(time());
echo "\n";
?>
We need to calculate the date of the first quarter first
$current_month = date('m');
// Get first month of quarter
$new_month = (3 * floor(($current_month - 1 ) / 3)) + 1;
// Add prefix zero if needed
$new_month = substr('0' . $new_month, -2);
$first_quarter_day_date = date('Y') . '-' . $new_month . '-01';
next we calculate the http://php.net/manual/en/datetime.diff.php
$datetime1 = new DateTime($first_quarter_day_date);
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->format('%a days');
<?php
function quarter_day($time = "") {
$time = $time ? strtotime($time) : time();
$date = intval(date("j", $time));
$month = intval(date("n", $time));
$year = intval(date("Y", $time));
// get selected quarter as number between 1 and 4
$quarter = ceil($month / 3);
// get first month of current quarter as number between 1 and 12
$fmonth = $quarter + (($quarter - 1) * 2);
// map days in a year by month
$map = [31,28,31,30,31,30,31,31,30,31,30,31];
// check if year is leap
if (((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)))) $map[1] = 29;
// get total number of days in selected quarter, by summing the relative portion of $map array
$total = array_sum(array_slice($map, ($fmonth - 1), 3));
// get number of days passed in selected quarter, by summing the relative portion of $map array
$map[$month-1] = $date;
$day = array_sum(array_slice($map, ($fmonth - 1), ($month - $fmonth + 1)));
return "Day $day on $total of quarter $quarter, $year.";
}
print(quarter_day("2017-01-01")) . "\n"; // prints Day 1 on 90 of quarter 1, 2017.
print(quarter_day("2017-04-01")) . "\n"; // prints Day 1 on 91 of quarter 2, 2017.
print(quarter_day("2017-08-15")) . "\n"; // prints Day 46 on 92 of quarter 3, 2017.
print(quarter_day("2017-12-31")) . "\n"; // prints Day 92 on 92 of quarter 4, 2017.
I've noticed that this thread went a bit beyond the question, and it's the first response to many google searches with "Quarter" & "PHP" in them.
If you're working with the ISO standards of organization, which you should if you're doing a business app, then
$curMonth = date("m", time());
$curQuarter = ceil($curMonth/3);
Is NOT correct, because the first day of a year in the ISO standards, can be 30, or 31 December.
Instead, you should use this :
$current_yearly_cycle_year_number = 2019;
$current_yearly_cycle_start->setISODate( $current_yearly_cycle_year_number, 1, 1 );
$current_yearly_cycle_end->setISODate( $current_yearly_cycle_year_number, 53, 1 );
if( $current_yearly_cycle_end->format("W") !== "53" )
$current_yearly_cycle_end->setISODate( $current_yearly_cycle_year_number, 52, 1 );
$week_number_start = intval( $current_yearly_cycle_start->format( "W" ) );
$timestamp_start_quarter = ( $week_number_start === 1 ? 1 : intval( ceil( $current_yearly_cycle_start->format( "m" ) / 3 ) ) );
var_dump( $timestamp_start_quarter );