i'm searching a way to display data for week and month.
For now, i can click on a day of the calendar and see the data related to this day.
For example, i'd like to choose the week number 5(or a month) of year 2013 and see the data related.
Any idea of how to do it?
Thank you.
i found out the solution:
My controller script:
$p = new DatePeriod(
new DateTime('2013-09-01'),
new DateInterval('P1W'),
new DateTime($today)
);
$data['weeks'] = array();
$data['month'] = array();
foreach ($p as $w) {
$data['weeks'][$w->format('W')] = $w->format('Y').' - Week '.$w->format('W');
$data['month'][$w->format('m')] = $w->format('Y').' - Month '.$w->format('m');
}
My view script:
<?php
// display for weeks
echo form_dropdown('week', $weeks, $lastWeek);
// display for month
echo form_dropdown('month', $month, $lastMonth);
?>
Related
I'm trying to create a list of the date of the first saturday each month.
Ideally I'd like it to show the next 6 dates.
I'm thinking of something like
function getSaturday($y, $m)
{
return new DatePeriod(
new DateTime("first saturday of $y-$m"),
DateInterval::createFromDateString('next saturday')
);
}
usage
foreach (getSaturday(2023, 11) as $saturday) {
echo $saturday->format("l, Y-m-d\n");
}
however this is code I have found and it currently just shows all the Saturdays in the next month. Can anyone help me out with the required changes please?
You can do with a simple loop.
const firstSaturday = 'first saturday of next month';
$months = [];
$month = new DateTime(firstSaturday);
for ($i = 0; $i < 6; $i++) {
$months[] = $month->format('Y-m-d');
$month->modify(firstSaturday);
}
print_r($months);
Output
2022-12-03
2023-01-07
2023-02-04
2023-03-04
2023-04-01
2023-05-06
I have a problem using PHP to get the finish date of course, as below:
Inputs:
Start date (e.g: 8/23/2019)
Schedule in week (e.g: Monday, Tuesday, Friday)
Total of lessons (e.g: 8)
Output: The finish date of course (is 9/9/2019 with above e.g inputs).
One lesson is one day. Input fields from end user:
Sorry for my bad English. Thank you very much!
As I see it loop while there are lessons left and subtract when it's Monday, Tuesday or Friday.
After the loop output the date.
$start = "8/23/2019";
$days = ["Monday", "Tuesday", "Friday"];
$n = 8;
$d = strtotime($start);
while($n>0){
//See if day is in days array
if(in_array(date("l", $d), $days)){
$n--;
}
$d += 86400; // go to next day
}
echo date("m/d/Y", $d-86400); //-86400 because the loop adds one at the end.
Try this code, if I understand you correctly))
<?
$startDay = '2019-09-25';
$aSchedule = array(1,2,4);
$iCntShed = count($aSchedule);
$iLessonsCnt = 8;
$iDWStartDay = date('w',strtotime($startDay));
$aDOWMap = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
// first day according Schedule array and startDay
$aWeekDays = array_filter($aSchedule,function($iSDW) use ($iDWStartDay){
return $iSDW>= $iDWStartDay;
});
// day according of week
$nextDate = date('Y-m-d',strtotime($startDay.' next '.$aDOWMap[end($aWeekDays)]));
$i = 0;
while (count($aWeekDays)<$iLessonsCnt) {
$i = $i<$iCntShed ? $i : 0;
$aWeekDays[] = $aSchedule[$i];
$nextDate = date('Y-m-d',strtotime($nextDate.' next '.$aDOWMap[$aSchedule[$i++]]));
}
print_r($aWeekDays);
echo $nextDate;
I'm working on a website where the user can create some events every X days (where X is the name of a day in the week). Then, he needs to enter the number of events he wants to create in the future.
For example, the user selects every Monday and Tuesday and decides to create 150 events.
Here is the code I have made until now :
// Init the date counter
$cpt_date_found = 0;
// Number of date to find
$rec_occ = 150;
// Init an ending date far in the future
$endDate = strtotime('+10 years', time());
// Loop over the weeks
for($i = strtotime('Monday', strtotime(date("d.m.Y"))); $i <= $endDate; $i = strtotime('+1 week', $i)) {
// -- Monday date found, create the event in the database
$cpt_date_found++;
// Break the loop if we have enough dates found
if($cpt_date_found == $rec_occ) {
break;
}
}
This code finds the date of every Monday in the future and breaks the loop once we have reached the number of occurrences the user specified.
I have entered an ending date far in the future to make sure I can break the loop before the end of the occurrences count specified by the user.
First I'm not sure about the "quality" of my code... I know that breaking the loop is not the best idea and am wondering if another solution would better fit my needs.
Then, instead of repeating the loop more times if the user specified several days (let's say, Monday, Tuesday and Friday), is there a way to loop one time for every provided days?
Thanks!
The following code will loop over a period of 5 years. For each week in those 5 years it will generate a DatePeriod containing each day of that week. It will compare each of those days to your preset array with days you are looking for. You can then generate your event after which the code will countdown for a certain amount of times. If the counter hits zero, you are done.
$searchDates = array('Mon', 'Tue', 'Fri');
$amountOfTimes = 27;
$startDate = new DateTime();
$endDate = new DateTime('next monday');
$endDate->modify('+5 years');
$interval = new DateInterval('P1W');
$dateRange = new DatePeriod($startDate, $interval ,$endDate);
// Loop through the weeks
foreach ($dateRange as $weekStart) {
$weekEnd = clone $weekStart;
$weekEnd->modify('+6 days');
$subInterval = new DateInterval('P1D');
// Generate a DatePeriod for the current week
$subRange = new DatePeriod($weekStart, $subInterval ,$weekEnd);
foreach ($subRange as $weekday) {
if (in_array($weekday, array('Mon', 'Fri', 'Sun'))) {
// Create event
// Countdown
$amountOfTimes--;
}
if ($amountOfTimes == 0) {
break;
}
}
}
I'm trying to create a calendar like page. A user can press a button with a yearnumber (i.e. 2017). When it's pressed, a session is set to 2017, the page reloads and the function week_date calculates the total number of weeks in the chosen year.
The same function is used for the months.
This works all fine, except for one thing. The first week and the last week of a year contains days from another year. For example: 2017 started on sunday. All other days (monday till saturday) where 2016. Now, my script doesn't change the year to 2016 for those days. It keeps 2017, so instead of
saturday 31-12-2016
sunday 01-01-2017
it echoes
saturday 31-12-2017
sunday 01-01-2017
Same goes for december and the remaining days of that week that are in january already.
What am I missing?
My code
$currentJaar = $_SESSION['currentJaar'];
if (!$currentJaar) {
$currentJaar = date("Y");
}
function week_date($week, $year, $dagvdweek){
$date = new DateTime();
return $date->setISODate($year, $week, $dagvdweek)->format('Y-m-d');
}
function getWeekDays($month, $year)
{
$p = new DatePeriod(
DateTime::createFromFormat('!Y-n-d', "$year-$month-01"),
new DateInterval('P1D'),
DateTime::createFromFormat('!Y-n-d', "$year-$month-01")->add(new DateInterval('P1M'))
);
$datesByWeek = array();
foreach ($p as $d) {
$dateByWeek[ $d->format('W') ][] = $d;
}
return $dateByWeek;
}
$datesByWeek = getWeekDays($maandNR, $currentJaar);
foreach ($datesByWeek as $week => $dates) {
$maandag = week_date($week,$currentJaar,1);
$dinsdag = week_date($week,$currentJaar,2);
$woensdag = week_date($week,$currentJaar,3);
$donderdag = week_date($week,$currentJaar,4);
$vrijdag = week_date($week,$currentJaar,5);
$zaterdag = week_date($week,$currentJaar,6);
$zondag = week_date($week,$currentJaar,7);
}
How to manipulate the date and exclude saturday and sunday?. The objective is, I need to create a cron job that will run and execute on datas that were created 5 days ago,"BUT", saturday and sunday shouldn't be included in that 5 days period.
here's what I have so far
$numdays = 5;
$today = strtotime('now');
$start = date('Y-m-d',strtotime('-'.$numdays.' day',$today));
echo $start;
if you try to run my code snippet above, it will show you the exact date 5 days ago 2016-02-10. But that one doesn't "exclude" saturday and sunday in the computation. it should be be 2016-02-08. So how to do that?
You can use PHP's date week of day, there are several versions, here is one using N:
<?php
$current = new DateTime();
$interval = new DateInterval('P1D');
$x = 5;
while ($x > 1) {
// Check if day of week is not saturday/sunday (1 => Monday ... 7 -> Sunday)
if ($current->format('N') >= 6) {
$x++;
}
$current->sub($interval);
$x--;
}
echo $current->format('Y-m-d') . PHP_EOL;
Example Run.
You can get a whole week and discard the weekends, keeping the furthest element in the array as a result.
$days = array_filter(array_map(function ($daysBack) {
$date = new \DateTimeImmutable("$daysBack days ago", new \DateTimeZone('UTC'));
return (!in_array($date->format('N'), [6, 7])) ? $date : null;
}, Range(1, 7)));
$fiveWorkingDaysAgo = end($days);