Skip weekends in php date - php

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 />';
}

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/>";
}
}

Date order 2 days ahead

$datetime = new \DateTime();
$listItem = array('<li">', '</li>');
$listItem_active = array('<li class="active-day">', '</li>');
$i = 0;
while (true) {
if ($i === 5) break;
if ($datetime->format('N') === '7' && $i === 0) {
$datetime->add(new \DateInterval('P1D'));
continue;
}
if($i===0){
$today = $datetime->format('D d-m');
}
if($i===3){
echo $listItem_active[0] . $today . $listItem_active[1];
}
if($i!=0){
echo $listItem[0] . $datetime->format('D d-m') . $listItem[1];
}
$listItem = array('<li>', '</li>');
$datetime->add(new \DateInterval('P1D'));
$i++;
}
I have the above code made and its almost right, but the output is not exactly how i want it. I get the following output:
The current day should always be in the middle. as you can see this works. but the order of the days is not as i desire. The order i desire is this :
You could use strtotime to get these results and the code will be much shorter.
For example:
for ($i = 2; $i > -3; $i--)
{
echo date('D d-m', strtotime($i . ' days ago')) . '<br />';
}
will output:
Tue 16-05
Wed 17-05
Thu 18-05
Fri 19-05
Sat 20-05

Display Next 15 days excluding sunday in 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 />";
}
}

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

confusing date manipulations

tying to go through all sundays like this:
/*self::WEEK = 604800*/
/* 3600 * 24 * 7 looks like 7 days for me*/
$start = 1286053200;
for ($i=0; $i < 10; $i++) {
echo date('d.m.Y H:i:s.u, D (z)', $start) . ' (' . $start . ')<br/>';
$start += self::WEEK;
}
but in some case i see this:
03.10.2010 00:00:00.000000, Sun (275) (1286053200)
10.10.2010 00:00:00.000000, Sun (282) (1286658000)
17.10.2010 00:00:00.000000, Sun (289) (1287262800)
24.10.2010 00:00:00.000000, Sun (296) (1287867600)
31.10.2010 00:00:00.000000, Sun (303) (1288472400)
06.11.2010 23:00:00.000000, Sat (309) (1289077200) <--
13.11.2010 23:00:00.000000, Sat (316) (1289682000)
20.11.2010 23:00:00.000000, Sat (323) (1290286800)
27.11.2010 23:00:00.000000, Sat (330) (1290891600)
04.12.2010 23:00:00.000000, Sat (337) (1291496400)
in other words lost an hour!? where?!
Try strtotime()
$start = 1286053200;
for ($i=0; $i < 10; $i++) {
echo date('d.m.Y H:i:s.u, D (z)', strtotime(' +' . ($i * 7) . ' DAYS',$start)) . ' (' . $start . ')<br/>';
}
Daylight saving?
Try gmdate() instead.
I created a function for a similar problem. Maybe you'll find it useful.
function getDays($year, $dayofweek, $format, $timezone='UTC')
{
$fridays = array();
$startDate = new DateTime("{$year}-01-01 {$dayofweek}", new DateTimezone($timezone));
$year++;
$endDate = new DateTime("{$year}-01-01", new DateTimezone($timezone));
$int = new DateInterval('P7D');
foreach(new DatePeriod($startDate, $int, $endDate) as $d) {
$fridays[] = $d->format($format);
}
return $fridays;
}
$sundays = getDays('2010', 'Sunday', 'd.m.Y H:i:s.u, D (z)', 'America/New_York');
var_dump($sundays);

Categories