Given start date get all the months name until current month? - php

Given start date how to get all the months until current month?
For eg. If given date is 1st Jan 2019
output should be:
Jan 2019
Feb 2019
Mar 2019
Apr 2019
May 2019
Jun 2019
Jul 2019
Aug 2019
Sep 2019
Oct 2019
Nov 2019
Dec 2019
Jan 2020
Feb 2020
Mar 2020
Solved it with slightly different approach. Thank you all for hints and answers.
<?php
$startTime = strtotime("1 January 2019");
$startYear = date("Y", $startTime);
$currentYear = date("Y");
$yearDiff = $currentYear - $startYear;
$currentMonth = date("m", time());
for($i=0; $i < intval($currentMonth) + ($yearDiff * 12) ; $i++)
{
$t = strtotime("+". $i . " months", $startTime);
$monthName = date("M", $t) ." ". date('Y', $t);
echo $monthName . "<br/>\n";
}
?>

here you go , hope this work
$start = new DateTime('2019-01-01');
$end = new DateTime('2020-03-02');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("F Y") . "<br>\n";
}

Related

How to get the name of the day in PHP using date?

Is it possible to get the name of the day in the row of numbers converted to date?
I have Code :
$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));
for($Date = 1; $Date <= $Maximum_Date; $Date++){
$DataDate = $Date . ' ' . $Month . ' ' . $Year . '<BR>';
echo $DataDate;
}
Result :
1 04 2019
2 04 2019
3 04 2019
etc..
What I want is to change it to display the name of the day on that date
For Example Date in April :
Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
etc..
[UPDATE] April 15, 2019
Refer to comments, I see documentation here and apply with mktime();
So I Update The Code :
$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));
for($Date = 1; $Date <= $Maximum_Date; $Date++){
echo date("l, d m Y", mktime(0, 0, 0, $Month, $Date, $Year)) . '<br>';
}
And get the result :
Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
etc..
You can simplify this a lot without converting back and forth between date and strtotime:
$year = date('Y');
$month = date('n');
$lastDay = date('t');
foreach (range(1, $lastDay) as $day) {
echo date('D, j m Y', mktime(0, 0, 0, $month, $day, $year)), '<br>';
}
See http://php.net/mktime.
Omitting implicit default values and condensing it a bit, you can in fact boil it down to:
foreach (range(1, date('t')) as $day) {
echo date('D, j m Y', mktime(0, 0, 0, date('n'), $day)), '<br>';
}
Mon, 1 04 2019
Tue, 2 04 2019
Wed, 3 04 2019
...
Tue, 30 04 2019
Note that this code has a minuscule potential to break, should you execute it right at the second in which one month rolls over to the next, and the date('n') and date('t') functions happen to be called "in different months". To avoid that possibility entirely, make this operation atomic:
list($year, $month, $lastDay) = explode(' ', date('Y n t'));
foreach (range(1, $lastDay) as $day) {
echo date('D, j m Y', mktime(0, 0, 0, $month, $day, $year)), '<br>';
}
The code below will print out what you need..
$date1 = date('Y-m-01');
$Maximum_Date = date('t', strtotime($date1));
for($Date = 1; $Date <= $Maximum_Date; $Date++)
{
$thatDay = date('Y-m-'.$Date);
$day = date('l, j', strtotime($thatDay));
$Month = date('m', strtotime($thatDay));
$Year = date('Y', strtotime($thatDay));
echo $day . ' ' . $Month . ' ' . $Year . '<BR>';
}
Output:
Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
Thursday, 4 04 2019
Friday, 5 04 2019
...
...
if you change this line
$day = date('l, j', strtotime($thatDay));
TO
$day = date('l, jS', strtotime($thatDay));
The output will be: Monday, 1st 04 2019
Use:
$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));
for($Date = 1; $Date <= $Maximum_Date; $Date++){
$DataDate = date("D-m-Y",strtotime($Year."-".$Month."-".$Date));
echo $DataDate. '<br>';;
}
You should try like below.
$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));
$StartDate=$date1;
$EndDate=($Year."-".$Month."-".$Maximum_Date);
$EndDate=date('Y-m-d',strtotime($EndDate));
while(strtotime($EndDate)>=strtotime($StartDate))
{
echo date('l, d-m-Y',strtotime($StartDate))."<br/>";
$StartDate=date('Y-m-d', strtotime($StartDate . ' +1 day'));
}
After execute you will get result like below.
Monday, 01-04-2019
Tuesday, 02-04-2019
Wednesday, 03-04-2019
...................
...................
...................
...................
Saturday, 27-04-2019
Sunday, 28-04-2019
Monday, 29-04-2019
Tuesday, 30-04-2019
Add the following line to your loop:
echo (new DateTime($Date. '-'. $Month .'-'. $Year))->format('l');
It creates a new DateTime object with your data & echo's the day in the format that you requested.

How to get every specific days of week within given date range in PHP?

This give me every monday date in date range.
Question: How to get every monday and friday of week?
$start_date = date('Y-m-d');
$end_date = date('Y-m-d', strtotime($start_date . ' + 1 MONTH'));
for(
$i = strtotime('Monday', strtotime($start_date));
$i <= strtotime($end_date);
$i = strtotime('+1 WEEK', $i)
) {
echo date('Y-m-d', $i). '<br>';
}
My Update:
$my_dates = [];
for(
$i = strtotime($start_date);
$i <= strtotime($end_date);
$i = strtotime('+1 DAY', $i)
) {
if(in_array(date('N', $i), array(1, 5))) {
$my_dates[] = date('Y-m-d', $i);
}
}
var_dump($my_dates);
Have a look at library called When, it's "Date / Calendar recursion library for PHP 5.3+".
Let's say MF schedule for next month:
$now = new DateTime('NOW');
$till = clone $now;
$till->modify('+1 month');
$r = new When();
$r->startDate($now)
->freq("weekly")
->until($till)
->byday(array('MO', 'FR'))
->generateOccurrences();
$occurrences = $r->occurrences;
If I'm not wrong than you can simply use for loop like as
$start = "2015-09-01";
$end = date('Y-m-d', strtotime("$start +1 months"));
$period = floor((strtotime($end) - strtotime($start))/(24*60*60));
for($i = 0; $i < $period; $i++){
if(in_array(date('l',strtotime("$start +$i day")),["Monday","Friday"]))
echo date('l d M, Y',strtotime("$start +$i day"))."\n";
}
Output:
Friday 04 Sep, 2015
Monday 07 Sep, 2015
Friday 11 Sep, 2015
Monday 14 Sep, 2015
Friday 18 Sep, 2015
Monday 21 Sep, 2015
Friday 25 Sep, 2015
Monday 28 Sep, 2015
Demo

Given a UNIX timestamp, how do I list all months and years from it till now in PHP?

I've this difficult problem. I've a unix timestamp, such as 1280102221, which corresponds to 25 Jul 2010. What I want to do is list all months/years since that day till now. In my example it would be:
Jul 2010
Aug 2010
Sep 2010
...
Oct 2014
Nov 2014
How do I do this?
Here is code I tried:
<?php
$timestamp = 1280102221;
$end = 1310000000;
for ($i = $timestamp; $i < $end; $i+= 24*30*3600) {
echo $i . "<br>";
}
?>
<?php
$timestamp = 1280102221;
$end = 1310000000;
$range = array();
while ($timestamp <= $end)
{
$range[] = date('M Y', $timestamp);
$timestamp = strtotime('+1 month', $timestamp);
}
print_r($range);
?>

PHP display all dates between a set of dates as list

Suppose I have 2 dates say 29 Aug 2014 and 3 Sep 2014. I need to display all dates between these to dates in the below format.
Aug 2014
29 Fri
30 Sat
31 Sun
Sept 2014
01 Mon
02 Tue
03 Wed
I know how to print all the dates like 29,30,31,1,2,3. But what I am unable to do is to get the month names in between.
Pretty easy question to be honest, pretty basic sollution possible..
$dateRange = new DatePeriod(
new DateTime('2014-07-28'),
new DateInterval('P1D'),
new DateTime('2014-08-04 00:00:01')
);
$month = null;
foreach ($dateRange as $date)
{
$currentMonth = $date->format('m Y');
if ($currentMonth != $month)
{
$month = $date->format('m Y');
echo $date->format('F Y').'<br />';
}
echo $date->format('d D').'<br />';
}
Above sollution results in:
July 2014
28 Mon
29 Tue
30 Wed
31 Thu
August 2014
01 Fri
02 Sat
03 Sun
Do mind it needs PHP >= 5.3 (due to the use of DatePeriod), but the actual logic to solve your question is easy to implement regardless of the used PHP version.
$timeS = strtotime("29 Aug 2014");
$timeE = strtotime("3 Sep 2014");
$monthS = -1;
$time = $timeS;
while ($time < $timeE) {
if ($monthS != date("n", $time)) {
echo date("M Y", $time) . "\n";
$monthS = date("n", $time);
}
echo date("d D", $time) . "\n";
$time = strtotime("+1 day", $time);
}
Edit: After doing it I'm pretty ok with #hindmost comment :)
I think , this is the complete code , as you wanted.
Executed code is here...
http://phpfiddle.org/main/code/3cbe-4855
<?php
$currentMonth = null;
$timeS = strtotime("29 Aug 2013");
$timeE = strtotime("3 Sep 2014");
$time = $timeS;
while ($time < $timeE) {
$month = date("M", $time);
$year = date("Y", $time);
if ($month != $currentMonth)
echo "<br /><h3>".$month."- ".$year."</h3>";
$currentMonth = $month;
echo "<br />".date("d D", $time);
$time = strtotime("+1 day", $time);
}
?>

How to show the past 24 months from now?

I need to write a simple loop routine to show 24 months back starting with today's month. How would I do that?
$start = date(M) - 24;
$end = date(M);
foreach() {
echo ''; // Dec, Jan...
}
Something like this should work:
for($i = 1; $i <= 24; $i++) {
echo date("M", strtotime("-$i months")) . "\n";
}
Result
Feb
Jan
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
Jan
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
See a demo
for ($i = 1; $i <= 24; $i++) {
$months[] = date("Y-m%", strtotime( date( 'Y-m-01' )." -$i months"));
}
or
for full textual representation of month you need to pass "F":
echo date("y:F:d");
for previous month you can use
echo date("y:F:d",strtotime("-24 Months"))

Categories