Display Next 15 days excluding sunday in php - php

How to Display Next 15 Days Without Saturday, Sunday in PHP. For Next 15 Days I am Using following Code. to Display Next 15 Days
for ($i = 1; $i <= 15; $i++) {
echo date('d/m/Y', strtotime('+'.$i.' day')) ."<br />";
}

You can use if statement
for ($i = 1; $i <= 15; $i++) {
$day = date('D', strtotime('+'.$i.' day'));
if($day != 'Sun' && $day != 'Sat'){
echo date('D d/m/Y', strtotime('+'.$i.' day')) ."<br />";
}
}

Related

Custom Month and Year inside php for-loop

I saw this question:
how to get the previous 3 months in php
My question is.. How do i output from a custom month.
I want to start from Mar 2018 (or any M Y user inputs) and it should output the next 3 (or any number user inputs) months.
Ex: Mar, Apr, May
The code below is from current month and year.
// current month: Aug 2018
for ($i = 0; $i <= 2; $i++){
$x = strtotime("$i month");
echo $dte = date('M Y', $x);
echo '<br>';
}
And the output is
Aug 2018
Sep 2018
Oct 2018
You could use the DateTime class and increment using a DateInterval object:
// Assuming these are the user inputs
$month = 11;
$year = 2015;
// We create a new object with year and month format
$date = DateTime::createFromFormat('Y m', $year . ' ' . $month);
for ($i = 0; $i <= 2; $i++){
// Output the month and year
echo $date->format('m Y') . '<br>';
// Add 1 month to the date
$date->add(new DateInterval('P1M'));
}
Output:
11 2015
12 2015
01 2016
Documentation:
DateTime::createFromFormat
DateTime::format
DateTime::add
Change it to this as below it will give you as expected see the below code
// current month: Aug 2018
$effective_date = "MAR 2018";
for ($i = 0; $i <= 2; $i++){
$x = strtotime("$i month",strtotime($effective_date));
echo $dte = date('M Y', $x);
echo '<br>';
}
This may be also helpful:
<?php
$month = 11;
$year = 2017;
$count = 15;
for ($i = 1; $i <= $count; $i++) {
$month++;
if ($month > 12) {
$month = 1;
$year++;
}
$x = DateTime::createFromFormat('Y-m', $year.'-'.$month);
echo $x->format('m-Y');
echo '<br>';
}
?>
Output:
12-2017
01-2018
02-2018
03-2018
04-2018
05-2018
06-2018
07-2018
08-2018
09-2018
10-2018
11-2018
12-2018
01-2019
02-2019
Try this code for add nth Days, Months and Years
$n = 2;
for ($i = 0; $i <= $n; $i++){
$d = strtotime("$i days");
$x = strtotime("$i month");
$y = strtotime("$i year");
echo "Dates : ".$dates = date('d M Y', "+$d days");
echo "<br>";
echo "Months : ".$months = date('M Y', "+$x months");
echo '<br>';
echo "Years : ".$years = date('Y', "+$y years");
echo '<br>';
}
You can generate the date with strtotime() function
Check this phpfiddle
<?php
$currMonth = 11;
$currYear = 2017;
$count = 15;
$currentYear = date('M Y',strtotime($currYear.'-'.$currMonth));
echo $currentYear.'<br>';
for ($i = 1; $i < $count; $i++) {
$currMonth++;
if ($currMonth > 12) {
$currMonth = 1;
$currYear++;
}
$newMonthandYear = date('M Y',strtotime($currYear.'-'.$currMonth));
echo $newMonthandYear.'<br>';
}
?>
<?php
$year = date('Y');
//start at march
$startMonth = 7;
//go forwards 2 months
$stepForwards = 22;
for ($i = $startMonth; $i <= $stepForwards; $i++){
if($i > 12 ) {
$month = $i % 12 == 0 ? 12 : $i % 12;
}
else {
$month = $i;
}
echo date("M Y", strtotime(date($year.'-'.$month.'-1'))) . "<br/>";
if($month == 12) {
$year++;
echo "<br/>";
}
}

Skip weekends in php date

I have this code :
for ($i = 2; $i > -3; $i--)
{
$class="";
if(date('D d-m')==date('D d-m', strtotime($i . ' days ago')))
{
$class=' class="distinct"';
}
echo '<li'.$class.'>'.date('D d-m', strtotime($i . ' days ago')) . '</li>'.'<br />';
}
And it outputs this:
Wed 17-05
Thu 18-05
Fri 19-05
Sat 20-05
Sun 21-05
I was wondering how i can skip the weekends, so it only shows the 5 days monday-friday. Any ideas how to exclude this?
You can start by creating a timestamp for monday and adding enough seconds to advance a day five times.
Like so:
$monday = strtotime('last monday');
for ($i = 0; $i < 5; $i++)
{
echo date('D d-m', $monday) . '<br />';
$monday = $monday + (60 * 60 * 24); // Add a day
}
I thing getDate() is just what you need. Where you can use 'wday' to check which day of the week it is (from 0 - Sunday, to 6 - Saturday). Example:
for ($i = 2; $i > -3; $i--)
{
$date = getDate(strtotime($i . ' days ago'));
if($date['wday'] == 6 || $date['wday'] == 0) {
echo "Weekend!\n";
}
else {
echo "Regular day...\n";
}
}
And it outputs:
Regular day...
Regular day...
Regular day...
Weekend!
Weekend!
Update:
for($i = -2; $i <= 2; $i++)
{
echo date ( 'D d-m' , strtotime ( "$i weekdays" ) ) . '<br>';
}
Will output:
Wed 17-05
Thu 18-05
Fri 19-05
Mon 22-05
Tue 23-05
===================================
Old answer:
$thisMonday = date("d-M-Y", strtotime('monday this week'));
for ($i = 0; $i <= 4; $i++)
{
$class="";
$date = date('d-M-Y', strtotime("+$i days", strtotime($thisMonday)));
if(date('D d-m') == date('D d-m', strtotime($i . ' days ago')))
{
$class=' class="distinct"';
}
echo '<li'.$class.'>'. date('D d-m', strtotime($date)) . '</li>'.'<br />';
}

PHP: get last 6 months in format month year

Is there a way in PHP to get the current and the previous 5 months in the following format?
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
Have you tried following:
<?php
echo date('F, Y');
for ($i = 1; $i < 6; $i++) {
echo date(', F Y', strtotime("-$i month"));
}
Let me know, if this wont work.
Do not use:
<?php
for ($i = 0; $i <= 6; $i++) {
echo date('F Y', strtotime(-$i . 'month'));
}
// With date e.g.: "May, 31", outputs:
// May, 2018, May 2018, March 2018, March 2018, January 2018, December 2017
You can fix it by:
<?php
for ($i = 0; $i <= 6; $i++) {
echo date('F Y', strtotime('last day of ' . -$i . 'month'));
}
Or better use DateTime, e.g.:
$dateTime = new DateTime('first day of this month');
for ($i = 1; $i <= 6; $i++) {
echo $dateTime->format('F Y');
$dateTime->modify('-1 month');
}
Try this
for ($j = 0; $j <= 5; $j++) {
echo date("F Y", strtotime(" -$j month"));
}
Why not use DateTime Object as
$start = new DateTime('first day of this month - 6 months');
$end = new DateTime('last month');
$interval = new DateInterval('P1M'); // http://www.php.net/manual/en/class.dateinterval.php
$date_period = new DatePeriod($start, $interval, $end);
$months = array();
foreach($date_period as $dates) {
array_push($months, $dates->format('F').' '.$dates->format('Y'));
}
print_r($months);
Use strtotime and date
for( $i = 0; $i <= 5 ; $i++) {
print date("F Y", strtotime("-".$i." month"))."\n";
}
to achieve another formats for date look PHP date format HERE
<?php
for ($i =0; $i < 6; $i++) {
$months[] = date("F Y", strtotime( date( 'Y-m-01' )." -$i months"));
}
print_r($months)
?>
Try This..
for ($i = 1; $i <= 6; $i++) {
$months[] = date("M-y", strtotime( date( 'Y-m-01' )." -$i months"));
}
print_r($months);
#php
for($i=0; $i<=5; $i++) {
$last_six_months[] =date("Y-m-d", strtotime( date( 'Y-m-01' )." -$i months"));
}
$last_six_months = array_reverse($last_six_months); // If you want to reverse...
#endphp
next >>>
#foreach($last_six_months as $key => $row)
<span>{{date('F Y',strtotime($row))}}</span>
#endforeach

how to get date format ( week 3 of month Jan) from week no

I have array of week numbers from 1 to 52. how i can convert it to
[week 1 jan],[week 2 jan] .......
using PHP
OK ... I fix it and here is my code
function getWeeks($date, $rollover)
{
$cut = substr($date, 0, 8);
$daylen = 86400;
$timestamp = strtotime($date);
$first = strtotime($cut . "00");
$elapsed = ($timestamp - $first) / $daylen;
$i = 1;
$weeks = 1;
for($i; $i<=$elapsed; $i++)
{
$dayfind = $cut . (strlen($i) < 2 ? '0' . $i : $i);
$daytimestamp = strtotime($dayfind);
$day = strtolower(date("l", $daytimestamp));
if($day == strtolower($rollover)) $weeks ++;
}
return $weeks;
}
and in the foreach I added
$x="1/1/2013 + ".$record->tms." weeks";
$m=date("Y-m-d", strtotime($x));
$first_week_start=getWeeks($m, "sunday");
if($first_week_start == 1){$typo="st";}
if($first_week_start == 2){$typo="nd";}
if($first_week_start == 3){$typo="rd";}
if($first_week_start == 4){$typo="th";}
if($first_week_start == 5){$typo="th";}
$month=date("M", strtotime($m));
$final_format .= "'".$first_week_start.$typo ." week in ".$month."'";
Try a loop, start with 1/jan, use DateInterval::createFromDateString('1 week'); and DateTime::add each time in the loop to add the next week, use DateTime::format to get the month also checking the current year in each iteration to make sure the loop hasn't moved to the next year.
Code:
date_default_timezone_set("UTC");
$weeks = array();
$dt = new DateTime("2013-01-01");
$interval = DateInterval::createFromDateString("1 week");
for($i=1; $i <= 52; $i++)
{
$weeks[$i] = "week " . $i . " " . $dt->format("M");
$dt = $dt->add($interval);
}
print_r($weeks);
If you want a function that will return a month abbreviation from a week:
function weekMonth($week)
{
date_default_timezone_set("UTC");
return (new DateTime("2013-01-01"))->add(DateInterval::createFromDateString($week." week"))->format("M");
}
echo weekMonth(6);

PHP - Get dates of next 5 weekdays?

I'm trying to create an array of the next 5 working week days (Monday - Friday, excluding today). I know the working week varies around the world but this is not important for what I am trying to do.
So, for example, if today is a Wednesday, I want the dates for Thursday and Friday of the current week and Monday, Tuesday and Wednesday of the following week.
I thought this would work:
$dates = array();
for ($i = 1; $ < 6; $i ++)
{
$dates[] = date('Y-m-d', strtotime('+ '.$i.' weekday'));
}
But for today, it is giving me:
Monday 1st
Tuesday 2nd
Wednesday 3rd
Thursday 4th
Sunday 7th!
Any advice appreciated.
Thanks
Try this
$dates = array();
$date = new DateTime();
while (count($dates)<5)
{
$date->add(new DateInterval('P1D'));
if ($date->format('N')<6)
$dates[]=$date->format('Y-m-d');
}
function getWeekdayDatesFrom($format, $start_date_epoch, $end_date_epoch, $range) {
$dates_arr = array();
if( ! $range) {
$range = round(abs($start_date_epoch-$end_date_epoch)/86400) + 1;
} else {
$range = $range + 1; //end date inclusive
}
$current_date_epoch = $start_date_epoch;
for($i = 1; $i <= $range; $i+1) {
$d = date('N', $current_date_epoch);
if($d <= 5) { // not sat or sun
$dates_arr[] = "'".date($format, $current_date_epoch)."'";
}
$next_day_epoch = strtotime('+'.$i.'day', $start_date_epoch);
$i++;
$current_date_epoch = $next_day_epoch;
}
return $dates_arr;
}
This should work:
$dates = array();
$i = 0;
while(true) {
$i++;
$time = strtotime("+$i days");
$dayOfWeek = date('w', $time);
/* 'w' - day of week, numeric, i.e. "0" (Sunday) to "6" (Saturday) */
if( ($dayOfWeek == 0) or ($dayOfWeek == 6) ) {
continue;
}
$dates[] = date('Y-m-d', $time);
if( count($dates) >= 5 ) {
break;
}
}
You can do like this:
$now = time();
$day = 60*60*24;
$days = array();
for ($i=1; $i<=7; $i++){
$ts = $now + $day*$i;
if (date("N", $ts) < 6){
$days[] = date("l jS", $ts);
}
}
The output for the above code is:
Array(
[0] => Monday 1st
[1] => Tuesday 2nd
[2] => Wednesday 3rd
[3] => Thursday 4th
[4] => Friday 5th
)

Categories