$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
Related
I'm trying to create an array of dates. The idea is that I add a number x days and the code, when adding the days skip only Sunday.
This is for laravel and I'm using carbon.
$date = Carbon::now();
$dates = [];
for($i = 1 ; $i < 20; $i++){
if($date->dayOfWeek === Carbon::SATURDAY){
echo $dates[$i] = $date->addDay(1)->format('d/m/Y') . " - Sunday <br> ";
} else {
echo $dates[$i] = $date->addDay(1)->format('d/m/Y') . "<br>";
}
When i use the constant SUNDAY to skip this date, its not working.
It goes on to consider Sunday as Monday
The problem is that you are checking if it's Saturday, and after that you're adding a day to it.
You need to echo the date before you add a day to it.
Try this:
if($date->dayOfWeek === Carbon::SUNDAY){ // checking if the current date is a sunday
echo $dates[$i] = $date->format('d/m/Y') . " - Sunday <br> "; // echo and add the current date to the array
$date->addDay(1);
} else {
echo $dates[$i] = $date->format('d/m/Y') . "<br>"; // echo and add the current date to the array
$date->addDay(1);
}
I got it with this code:
$inicialDate = Carbon::now();
$newDate = [];
for($i = 1; $i < 30; $i++)
{
$newDate[$i] = $inicialDate->addDay(1);
if($newDate[$i]->format('l') == "Sunday")
{
$newDate[$i] = $inicialDate->addDay(1);
}
echo $newDate[$i]->format('d/m/Y') . " - " . $newDate[$i]->format('l') . "<br>";
}
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 />';
}
I have a while loop that shows 5 days. The first day is the current day and the other ones are the next 4. This is my code:
$datetime = new \DateTime();
$listItem = array('<li class="active">', '</li>');
$i = 0;
while (true) {
if ($i === 5) break;
if ($datetime->format('N') === '7' && $i === 0) {
$datetime->add(new \DateInterval('P1D'));
continue;
}
echo $listItem[0] . $datetime->format('D d-m') . $listItem[1];
$listItem = array('<li>', '</li>');
$datetime->add(new \DateInterval('P1D'));
$i++;
}
The result looks like this:
My problem is, I want the current day to be in the middle. Where saturday is should be the current day. Do you know how to do this?
Thanks in advance.
Please Try Below Code :
$datetime = new \DateTime();
$listItem = array('<li">', '</li>');
$listItem_active = array('<li class="active">', '</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++;
}
$datetime = new \DateTime();
$datetime->modify('-2 Day');
http://php.net/manual/en/datetime.modify.php
You could simply subtract 2 days from your starting day like this.
$datetime = new \DateTime();
$datetime->sub(new \DateInterval('P2D'));
I am working on a "cut-off" date and I need to set it on every month. Say, I set the cut-off date to August 25 for this year, then it should be September 25, October 25 and so on till the year ends.
Here's the code I have:
$now = "2015-08-25";
$nextDate = getCutoffDate($now);
echo $nextDate;
function getCutoffDate($start_date)
{
$date_array = explode("-",$start_date); // split the array
// var_dump($date_array);
$year = $date_array[0];
$month = $date_array[1];
$day = $date_array[2];
/*if (date('n', $now)==12)
{
return date("Y-m-d",strtotime(date("Y-m-d", $start_date) . "+1 month"));
}*/
if (date("d") <= $day) {
$billMonth = date_format(date_create(), 'm');
}
else{
$billMonth = date_format(date_modify(date_create(), 'first day of next month'), 'm');
}
// echo date("d").' '. $billMonth.'<br>';
$billMonthDays = cal_days_in_month(CAL_GREGORIAN, ($billMonth), date("Y"));
// echo $billMonthDays.'<br>';
if ($billMonthDays > $day) {
$billDay = $day;
} else {
$billDay = $billMonthDays;
}
}
I got this from here: http://www.midwesternmac.com/blogs/jeff-geerling/php-calculating-monthly
It returns the same date for the next month only, but how do I get the specific date of EACH month of the current year? Kindly leave your thoughts. Still a newbie here, sorry.
In that case, this should be enough:
<?php
for($i=8; $i<=12; $i++) {
echo sprintf('25-%02d-2015', $i) . '<br>';
}
but if you need more flexible way:
<?php
$date = new DateTime('25-08-2015');
function getCutoffDate($date) {
$days = cal_days_in_month(CAL_GREGORIAN, $date->format('n'), $date->format('Y'));
$date->add(new DateInterval('P' . $days .'D'));
return $date;
}
for($i = 0; $i < 5; $i++) {
$date = getCutoffDate($date);
echo $date->format('d-m-Y') . '<br>';
}
This should print:
25-09-2015
25-10-2015
25-11-2015
25-12-2015
25-01-2016
I have the following code that I created to generate a Calendar, but it has some issues:
//Labels
$dayLabels = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$monthLables = array("January","February","March","April","May","June","July","August","September","October","November","December");
//max values
$maxDays = 7;
$maxMonths = 12;
//stats
$forceMonth = $_GET['m'];
$forceYear = $_GET['y'];
$todayDate = date("d-m-Y");
$todayDate = date("d-m-Y", strtotime($todayDate));
$explodeToday = explode("-", $todayDate);
$currentDay = $explodeToday[0];
if(isset($forceMonth)) {
$currentMonth = $forceMonth;
} else {
$currentMonth = $explodeToday[1];
};
if(isset($forceYear)) {
$currentYear = $forceYear;
} else {
$currentYear = $explodeToday[2];
};
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);
//database values
$startDate = array("01-06-2015","25-06-2015");
$endDate = array("05-06-2015","05-07-2015");
$bookedUser = array("Dexter","James");
//counters
$daysIntoMonth = 0;
$dayCounter = 0;
//debug
echo '<p>Current Month: ' .$monthLables[$currentMonth-1]. ' / ' .$currentMonth. '</p>';
echo '<p>Current Year: ' .$currentYear. '</p>';
//start of Calendar
echo '<table>';
//print days of week
echo '<tr>';
foreach($dayLabels as $day) {
echo '<td style="border-bottom:dashed 1px #DDD;">' .$day. '</td>';
};
echo '</tr>';
while($daysIntoMonth < $daysInMonth) {
//days into month
$daysIntoMonth++;
$temp_inMonth = sprintf("%02d", $daysIntoMonth);
$daysIntoMonth = $temp_inMonth;
//days into week
$dayCounter++;
$temp_dayCounter = sprintf("%02d", $dayCounter);
$dayCounter = $temp_dayCounter;
//current calendar date
$calDate = date('d-m-Y', strtotime($daysIntoMonth. '-' .$currentMonth. '-' .$currentYear));
$timeCal = strtotime($calDate);
if($dayCounter == 1) {
echo '<tr>';
};
if($startKey = array_search($calDate, $startDate) !== FALSE) {
$booked = true;
};
if($endKey = array_search($calDate, $endDate) !== FALSE) {
$booked = false;
};
if($booked == true) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else if($booked == true && array_search($calDate, $startDate) !== FALSE) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else if($booked == false && array_search($calDate, $endDate) !== FALSE) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else {
echo '<td>' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
}
if($dayCounter == $maxDays) {
echo '</tr>';
$dayCounter = 0;
};
};
//table is kill
echo '</table>';
The issues I have noticed:
Unable to put a $bookedUser for respective $startDate,$endDate.
When a booking laps over to another month, it skips all the dates until the $endDate.
All Months start on Monday, how would I go about making them start of correct days of the week.
Possible code examples to help me solve my issues would be great, thanks in advance.
Edit:
I have solved problem 3 by using the following code:
$firstDayofMonth = strtotime("01-$currentMonth-$currentYear");
$firstDayofMonth = date("D", $firstDayofMonth);
$firstDayofMonth = array_search($firstDayofMonth, $dayMiniLabels);
$firstDayofMonth = $firstDayofMonth + 1;
$startMonth = 0;
if($firstDayofMonth != 7) {
while($startMonth < $firstDayofMonth) {
echo '<td></td>';
$startMonth++;
$dayCounter++;
$temp_dayCounter = sprintf("%02d", $dayCounter);
$dayCounter = $temp_dayCounter;
};
};
For the days and months (problem 3), I would do this:
$todaysNumber = date('w');
$currentDayInText = $dayLabels[$todaysNumber];
And the same for the monhts.
Mostly, in my MySQL-tables, dates are placed like 2015-06-05 and not in European time notation. Maybe that could solve problem 1?