I am trying to build a loop that echoes each month of the year until the current month. My foreach loop just stays on the start and doesn't progress through the rest of the range. Here is my code:
$startMonth = '01';
$endMonth = date('m');
foreach (range($startMonth, $endMonth) as $month) {
$dataValue = date('n', $month);
echo '<td class="month" data-value="';
echo $dataValue;
echo '" data-id="' . $incrementYear . '"><a href="/Archive/view/';
echo $incrementYear;
echo '/';
echo date('m', $month);
echo '">';
echo date('F', $month);
echo '</a></td>';
}
Here is the output:
January January January January January January
Each of the months has the correct link for what January should have, but for some reason, the range never leaves "01" which is the $startMonth and I'm not sure why. I was thinking it had something to do with integer vs. string, but removing the ' from $startMonth = '01'; to make it an integer, but that didn't work either.
As far as I understand them, I am using the range correctly.
What am I missing here?
EDIT
$dataValue can be commented out and I will still get January 6 times, its just the link will have a data-value different than what I want. This variable has no effect on the month itself.
If I change the bottom line of code from echo date('F', $month); to echo $month; I get:
1 2 3 4 5 6
Problem is that to date() as timestamp you always pass 01, 02... and that converts to ~12 first seconds of Unix timestamp. So 1970-01-01 00:00:03 is January.
Change your code to use mktime
$dataValue = date('n', mktime(0, 0, 0, $month, 1, date('Y')));
$startMonth = '01';
$endMonth = date('m');
foreach (range($startMonth, $endMonth) as $month) {
$time = mktime(0, 0, 0, $month, 1, date('Y'));
$date = date('n', $time);
$month = date('m', $time);
$monthName = date('F', $time);
echo "<td class='month' data-value='{$date}' data-id='{$incrementYear}'><a href='/Archive/view/{$incrementYear}/{$month}'>{$monthName}</a></td>";
}
Try this:
$startMonth = '01';
$endMonth = date('m');
foreach (range($startMonth, $endMonth) as $month) {
$dataValue = date('F', mktime(0, 0, 0, $month, 1, date('Y')));
$month = sprintf('%02d', $month);
echo '<td class="month" data-value="' . $dataValue . '" data-id="' . $month . '">' . $dataValue . '</td>';
}
Output:
JanuaryFebruaryMarchAprilMayJune
Change the $dataValue from
$dataValue = date('n', $month);
to
$dateObj = DateTime::createFromFormat('!m', $month);
echo $dateObj->format('F');
Hope this helps.
Try Below Code:
$start_date = date('Y-01-01');
$end_date = date('Y-m-01');
$loop_date = $start_date;
//loop through months
while(strtotime($loop_date)<=strtotime($end_date)){
echo date('F',strtotime($loop_date));
$loop_date = date('Y-m-d', strtotime("+1 months", strtotime($loop_date)));
}
Related
I have a range of years, I want to know the first and last day or date of each year. because I need to calculate all activities that have been done in each years. here is my code:
$years = range(2015, date('Y'));
foreach ($years as $year) {
echo "Number of activities" . $year . "<br/>";
}
I need to have the strat and end date or day of all years.
I can get the first and last day in the way as below but it not a good salution:
// for 2015
$firstDayOfYear = date('Y-m-d', strtotime('first day of january 2015'));
$lastDayOfYear = date('Y-m-d', strtotime('last day of december 2015'));
// for 2016
$firstDayOfYear = date('Y-m-d', strtotime('first day of january 2016'));
$lastDayOfYear = date('Y-m-d', strtotime('last day of december 2016'));
.
.
.
// until 2021
When the number of year increase it's not possible manually. Because i need to display them in a chart.
Can anybody help me pelease ?
Check this out
$start_year = '2015';
$last_year = date('Y');
for($start_year; $start_year <= $last_year; $start_year++){
echo $firstDayOfYear = date('Y-m-d', strtotime("first day of january {$start_year}"));
echo $lastDayOfYear = date('Y-m-d', strtotime("last day of december {$start_year}"));
}
Try this solution:
$years = range(2015, date('Y'));
$array = [];
foreach ($years as $year) {
echo "Number of activities " . $year . "<br/>";
$obj = (object)[];
$obj->$firstDayOfYear = date('Y-m-d', strtotime('first day of january ' . $year));
$obj->$lastDayOfYear = date('Y-m-d', strtotime('last day of december ' . $year));
$array[$year] = $obj;
}
var_dump($array);
I'm trying to go back one day in the calendar – What am I doing wrong?
My approach:
$weekdays = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
///////
function getDates($year) {
$dates = array();
date("L", mktime(0, 0, 0, 7, 7, $year)) ? $days = 366 : $days = 365;
///
for($i = 1; $i <= $days; $i++) {
// get Unix timestamp for a date
$month = date('F', mktime(0, 0, 0, 1, $i, $year));
$wk = date('W', mktime(0, 0, 0, 1, $i, $year));
$wkDay = date('D', mktime(0, 0, 0, 1, $i, $year));
$day = date('d', mktime(0, 0, 0, 1, $i, $year));
///
$dates[$month][$wk][$wkDay] = $day;
}
return $dates;
}
//////
$year = 2020; // from this Year
$dates = getDates($year);
$countW = 1; // for counting weeks
// Output code
echo '<h2><span>Calendar '.$year.'</span></h2>';
foreach($dates as $month => $weeks) {
echo '<div>';
echo '<table>';
echo '<caption>'.$year.' '.$month.'</caption>';
echo '<tr>';
echo '<th>W</th>';
echo '<th>'.implode('</th><th>', $weekdays).'</th>';
echo '</tr>';
echo '<tr>';
foreach ($weeks as $week => $days){
echo '<tr><td>'.$countW++.'</td>';
foreach ($weekdays as $day) {
echo '<td>';
/* I'm overwhelmed here! */
echo isset($days[$day]) ? $days[$day] : date('d', strtotime('-1 day')); // outputted is 5
echo '</td>';
}
echo '</tr>';
}
echo '</tr>';
echo '</table>';
}
I tried the following codes:
date('d', strtotime('-1 day')) // outputted: 5 (today is 6)
$jd = gregoriantojd(01,06,2020); echo jddayofweek($jd,2); // outputted: Mon (today is Tue)
With this code I get the day from the current day before.
It is expected the day before the specified day
what is happening
is expected
With this code I get the day from the current day before. It is expected the day before the specified day
The day that is "specified" here date('d', strtotime('-1 day')) is the current day.
Generally speaking, a (American) monthly calendar will leave a blank space if the day is not in the month. For example:
That would be a super simple fix to the problem. If not, you'll need to adjust your thinking (and the code!). In the case of the first week of January, the dates for Mon and Tues are -2 and -1 from the first day of January. Similarly, in the last week they are -2 and -1 from the first day of February (or +1, +2 from the last day of January).
I am trying to get third previous month from inputted month-year ie. if I give 06-2016 as an input, it should give me 03-2016 as result. I have tried strtotime("-3 Months"). But it gives me just 3 months from current date time.
Can someone tell me how to solve it please.
I have tried:
$month = 06;
$year = 2016;
$d = mktime(0, 0, 0, $month, 1, $year);
$td = date("Y-m-d", $d);
$oldDate = date('Y-m-d', strtotime("-3 Months"));
It gives me 3 months from current date. Any guesses how do I get the expected result ?
Use below. You need to use date("F Y", strtotime( INPUT_DATE ." -3 months"))
$month = 06;
$year = 2016;
$d = mktime(0, 0, 0, $month, 1, $year);
$td = date("Y-m-d", $d);
echo $olddate = date("Y-m-d", strtotime( $td ." -3 months"));
echo $months = date("F Y", strtotime( $td ." -3 months"));
Output:
2016-03-01
March 2016
Online Demo: Click Here
strtotime of (Input Date ."-3 Months")
$month = 06;
$year = 2016;
$d = mktime(0, 0, 0, $month, 1, $year);
$td = date("Y-m-d", $d);
$oldDate = date('Y-m-d', strtotime($td."-3 Months"));
$oldDate_MonthYear = date('m-Y', strtotime($td."-3 Months"));
echo $oldDate;
echo '<br />';
echo $oldDate_MonthYear;
Use Object Based.
$date1 = new DateTime("2016-06-01");
echo $startDate = $date1->modify("-3 month")->format('Y-m-d');
Try this and you should get desired result.
$prev_month_ts = strtotime('2016-06 -3 month');
$prev_month = date('Y-m', $prev_month_ts);
echo $prev_month;
I wish to rewrite a mysql query which use month() and year() functions to display all the posts from a certain month which goes to my function as a 'Y-m-d' parameter format, but I don't know how can I get the last day of the given month date.
$query_date = '2010-02-04';
list($y, $m, $d) = explode('-', $query_date);
$first_day = $y . '-' . $m . '-01';
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));
I know this question has a good answer with 't', but thought I would add another solution.
$first = date("Y-m-d", strtotime("first day of this month"));
$last = date("Y-m-d", strtotime("last day of this month"));
Try this , if you are using PHP 5.3+, in php
$query_date = '2010-02-04';
$date = new DateTime($query_date);
//First day of month
$date->modify('first day of this month');
$firstday= $date->format('Y-m-d');
//Last day of month
$date->modify('last day of this month');
$lastday= $date->format('Y-m-d');
For finding next month last date, modify as follows,
$date->modify('last day of 1 month');
echo $date->format('Y-m-d');
and so on..
cal_days_in_month() should give you the total number of days in the month, and therefore, the last one.
// First date of the current date
echo date('Y-m-d', mktime(0, 0, 0, date('m'), 1, date('Y')));
echo '<br />';
// Last date of the current date
echo date('Y-m-d', mktime(0, 0, 0, date('m')+1, 0, date('Y')));
$month = 10; // october
$firstday = date('01-' . $month . '-Y');
$lastday = date(date('t', strtotime($firstday)) .'-' . $month . '-Y');
Basically:
$lastDate = date("Y-m-t", strtotime($query_d));
Date t parameter return days number in current month.
Print only current month week:
function my_week_range($date) {
$ts = strtotime($date);
$start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
echo $currentWeek = ceil((date("d",strtotime($date)) - date("w",strtotime($date)) - 1) / 7) + 1;
$start_date = date('Y-m-d', $start);$end_date=date('Y-m-d', strtotime('next saturday', $start));
if($currentWeek==1)
{$start_date = date('Y-m-01', strtotime($date));}
else if($currentWeek==5)
{$end_date = date('Y-m-t', strtotime($date));}
else
{}
return array($start_date, $end_date );
}
$date_range=list($start_date, $end_date) = my_week_range($new_fdate);
## Get Current Month's First Date And Last Date
echo "Today Date: ". $query_date = date('d-m-Y');
echo "<br> First day of the month: ". date('01-m-Y', strtotime($query_date));
echo "<br> Last day of the month: ". date('t-m-Y', strtotime($query_date));
In case of name of month this code will work
echo $first = date("Y-m-01", strtotime("January"));
echo $last = date("Y-m-t", strtotime("January"));
I need to get the first and last day of a month in the format YYYY-MM-DD given only the month and year. Is there a good, easy way to do this?
$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.
First day is always YYYY-MM-01, isn't it? Example: date("Y-M-d", mktime(0, 0, 0, 8, 1, 2008))
Last day is the previous day of the next month's first day:
$date = new DateTime("2008-09-01");
$date->modify("-1 day");
echo $date->format("Y-m-d");
The first day of the month is always 1.
So it will become
YYYY-MM-01
the last day can be calculated as:
<?php
$num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
echo "There was $num days in August 2003";
?>
OK, first is dead easy.
date ('Y-m-d', mktime(0,0,0,MM,01,YYYY));
Last is a little trickier, but not much.
date ('Y-m-d', mktime(0,0,0,MM + 1,-1,YYYY));
If I remember my PHP date stuff correctly...
**edit - Gah! Beaten to it about a million times...
Edit by Pat:
Last day should have been
date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1
<?php
echo "Month Start - " . $monthStart = date("Y-m-1") . "<br/>";
$num = cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y"));
echo "Monthe End - " . $monthEnd = date("Y-m-".$num);
?>
The easiest way to do this with PHP is
$dateBegin = strtotime("first day of last month");
$dateEnd = strtotime("last day of last month");
echo date("MYDATEFORMAT", $dateBegin);
echo "<br>";
echo date("MYDATEFORMAT", $dateEnd);
Or the last week
if (date('N', time()) == 7) {
$dateBegin = strtotime("-2 weeks Monday");
$dateEnd = strtotime("last Sunday");
} else {
$dateBegin = strtotime("Monday last week");
$dateEnd = strtotime("Sunday last week");
}
Or the last year
$dateBegin = strtotime("1/1 last year");
$dateEnd = strtotime("12/31 this year");
By the way #ZombieSheep solution
date ('Y-m-d', mktime(0,0,0,$MM + 1,-1,$YYYY));
does not work it should be
date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1
Of course #Michał Słaby's accepted solution is the simplest.
Just to verify that I didn't miss any loose ends:
$startDay = 1;
if (date("m") == 1) {
$startMonth = 12;
$startYear = date("Y") - 1;
$endMonth = 12;
$endYear = date("Y") - 1;
}
else {
$startMonth = date("m") - 1;
$startYear = date("Y");
$endMonth = date("m") - 1;
$endYear = date("Y");
}
$endDay = date("d") - 1;
$startDate = date('Y-m-d', mktime(0, 0, 0, $startMonth , $startDay, $startYear));
$endDate = date('Y-m-d', mktime(0, 0, 0, $endMonth, $endDay, $endYear));
try this to get the number of days in the month:
$numdays = date('t', mktime(0, 0, 0, $m, 1, $Y));
Example; I want to get first day and last day of current month.
$month = (int) date('F');
$year = (int) date('Y');
date('Y-m-d', mktime(0, 0, 0, $month + 1, 1, $year)); //first
date('Y-m-d', mktime(0, 0, 0, $month + 2, 0, $year)); //last
When you run this for instance at date 2015-01-09, the first and last values will be sequentially;
2015-01-01
2015-01-31
Tested.
From here(get next month last day) that is marked as duplicated, so i can't add comment there, but people can got bad answers from there.
Correct one for last day of next month:
echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-t'));
Correct one for first day of next month:
echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-01'));
Code like this will be providing March from January, so that's not what could be expected.
echo ((new DateTime())->modify('+1 month')->format('Y-m-t'));
proper way to build a relative date from now is:
//bad example - will be broken when generated at 30 of December (broken February)
echo date("Y-m-d", strtotime("now"))."\n";
echo date("Y-m-d", strtotime("now + 1 month"))."\n";
echo date("Y-m-d", strtotime("now + 2 month"))."\n";
echo date("Y-m-d", strtotime("now + 3 month"))."\n";
//good example, you can change first day to last day or any day
echo date("Y-m-d", strtotime("first day of this month"))."\n";
echo date("Y-m-d", strtotime("first day of next month"))."\n";
echo date("Y-m-d", strtotime("first day of +2 month"))."\n";
echo date("Y-m-d", strtotime("first day of +3 month"))."\n";
and the result will be:
2021-12-30
2022-01-30
2022-03-02
2022-03-30
2021-12-01
2022-01-01
2022-02-01
2022-03-01