PHP - Matching Day Of Week And Time Of Day
I wish to find the closest two DateTimes which are within $fillStart and $fillEnd and have the same week and 24 hour times as $gapStart and $gapEnd. For instance, the following results in $fillStartModified and $fillEndModified which meets that criteria. fillStart 2019-07-23 00:15:00 Tue fillEnd 2019-09-23 13:00:00 Mon gapStart 2019-05-23 00:15:00 Thu gapEnd 2019-06-23 13:00:00 Sun fillStartModified 2019-07-25 00:15:00 Thu fillEndModified 2019-08-25 13:00:00 Sun I came up with the following which seems to work, but it is kind of complicated and I am not positive it will meet all edge conditions. Any recommendations? Thanks function getFill(\DateTimeImmutable $fillStart, \DateTimeImmutable $fillEnd, \DateTimeInterface $gapStart, \DateTimeInterface $gapEnd) { //$gapInterval = $gapStart->diff($gapEnd); // Doesn't work $gapInterval = new \DateInterval('PT'.($gapEnd->getTimestamp() - $gapStart->getTimestamp()).'S'); if($gapStart > $fillStart) { //The fill is older than the gap so make the fill's endTime match the gap's endTime $fillEndModified = $fillEnd ->modify('previous '.$gapEnd->format('D')) ->setTime(...explode('.', $gapEnd->format('H.i.s.u'))); if($fillEndModified->diff($fillEnd)->format('%a') >= 7) { $fillEndModified = $fillEndModified->add(new \DateInterval('P7D')); } $fillStartModified = $fillEndModified->sub($gapInterval); if($fillStartModified < $fillStart) { $fillStartModified=null; $fillEndModified=null; } } else { //The fill is newer than the gap so make the fill's startTime match the gap's startTime $fillStartModified = $fillStart ->modify('next '.$gapStart->format('D')) ->setTime(...explode('.', $gapStart->format('H.i.s.u'))); if($fillStart->diff($fillStartModified)->format('%a') >= 7) { $fillStartModified = $fillStartModified->sub(new \DateInterval('P7D')); } $fillEndModified = $fillStartModified->add($gapInterval); if($fillEndModified > $fillEnd) { $fillStartModified=null; $fillEndModified=null; } } return [$fillStartModified, $fillEndModified]; }
Similar TutorialsHello there, I have a DB table where everyday I insert my meetings for the future. I want to ask you guys if anybody can help me with creating some sort of a weekly report. EXAMPLE: I have meeting from the beginning of the year (Mon-Sat)...could be 4-10 meeting per day, so a lot in general. I want them to appear , with sql, under the WEEK they were in for example: (a year has 52 weeks) WEEK 1 (01-01-2011 -> 08-01-2011) *M1 *M2 *M3 ... WEEK 2 (09-01-2011 -> 16-01-2011) *M4 *M5 *M6 ... etc... Is there a way to define the date's range after the week ended automatically? Thanks I'm trying to produce a weekly calendar. I can't figure how to go about getting the sundays of particular weeks of the year. I know you can get the week number( date('W') produces 1 - 52) of a particular date but i can't really figure how to use that number to get the sunday in that week. Anybody have any suggestions? It would be nice if i could write a function where i input the week number and the year and it returns the sunday of that week. Can't figure out how to do that though. Hello All, I have read through this script a million times, and I can not for the life of me figure out how the script decides which day of the week to start on. Right now the week starts on Monday (which goes against every other calendar we use). I need to figure out how to make the week start on Sunday. Here's the script: <style type='text/css'> .outofrange { background-color: #d9cdb3; } .currentday { background-color: #ccc; } </style> <?PHP $Year = preg_replace("/[^0-9]/","",$_GET['year']); $Month = preg_replace("/[^0-9]/","",$_GET['month']); if ($Month == "") { $Month = date("n"); } if ($Year == "") { $Year = date("Y"); } if ($Month==0) { $Month=12; $Year--; } if ($Month==13) { $Month=1; $Year++; } $myServer = "xxxxxx"; $myUser = "xxxxxx"; $myPass = "xxxxxx"; $myDB = "Calendar"; //connection to the database $dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer"); //select a database to work with $selected = mssql_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB"); //declare the SQL statement that will query the database $query = " SELECT * FROM calendar WHERE school = '00' "; //execute the SQL query and return records $result = mssql_query($query); //display the results while($row = mssql_fetch_array($result)) { if (date("Y",strtotime($row['date']))==$Year) { $Event[date("z",strtotime($row['date'])+1)+1] .= $row['event'] . "<br />" . "<font color='#" . $row['colors'] . "'>" . $row['location'] . "</font>" . "<p>"; } } $Timestamp = strtotime("$Year-$Month-01"); for ($Day=1;$Day<=32;$Day++) { if (checkdate($Month,$Day,$Year)) { $LastDayInMonth = $Day; } } $FirstDayInMonth = date("N",$Timestamp); for ($MakeCal=$FirstDayInMonth;$MakeCal<=$FirstDayInMonth+$LastDayInMonth;$MakeCal++) { //echo "$MakeCal<br />"; } $WeeksNeeded = ceil(($FirstDayInMonth+$LastDayInMonth)/7); echo "<div align='center'><span class='bodyb'>" . date("F Y",$Timestamp) . "</span></div><p>"; echo "<table width='100%' border='0'> <tr> <td align='left' valign='top'><a href='index.php?month=" . (($Month)-1) . "&year=$Year' class='monthview'>Previous Month</a></td> <td align='right' valign='top'><a href='index.php?month=" . (($Month)+1) . "&year=$Year' class='monthview'>Next Month</a></span></td> </tr> </table>"; echo "<p>"; //Main Table starting with days of week through the month views// echo "<table width='615px' cellspacing='0' cellpadding='2' border='1' bordercolor='#666666'> <tr> <th>Monday</th> <th>Tuesday</th> <th>Wednesday</th> <th>Thursday</th> <th>Friday</th> <th>Saturday</th> <th>Sunday</th> </tr> "; for ($DoWeeks=1;$DoWeeks<=$WeeksNeeded;$DoWeeks++) { for ($DoDays=1;$DoDays<=7;$DoDays++) { $CurrentDay++; if ($CurrentDay < $FirstDayInMonth) { echo "<td width='10%' class='outofrange'> </td>"; } else { if (($CurrentDay-$FirstDayInMonth)+1>$LastDayInMonth) { echo "<td width='10%' class='outofrange'> </td>"; } else { $DayOfYear = date("z",strtotime("$Year-$Month-" . (($CurrentDay-$FirstDayInMonth)+1))) + 1; if ($DayOfYear==date("z")+1 && date("Y")==$Year) { echo "<td width='10%' height='100' valign='top' class='currentday'><strong>" . (($CurrentDay-$FirstDayInMonth)+1) . "</strong><br />"; } else { echo "<td width='10%' height='100' valign='top'><strong>" . (($CurrentDay-$FirstDayInMonth)+1) . "</strong><br />"; } if ($Event[$DayOfYear]!="") { echo $Event[$DayOfYear] . $Location[$DayOfYear] . "<p /></font>"; } echo "</td>"; } } } echo "</tr><tr>"; } echo "</table><p>"; Hi, I'm trying to alter the code below. It cuurently finds the start and end date of this week, no matter what day you're on. What I now need is to find the start & end date of next week, again, no matter what day you're on of this week Thanks to aleX_hill for the following code: <?php #THIS WEEK $dayOfWeek = date("w"); #The number of the day of the week. Sun = 0, Sat = 1 $todayDayOfMonth = date("d"); #The day of the month $thisMonth = date("m"); #The number of this month. Jan = 1, Dec = 12 $firstDayOfWeek = $todayDayOfMonth - $dayOfWeek; #The day of the month for the beginning of the week (Sun) if($firstDayOfWeek < 1) #The beginning of the week was in the previous month { $monthBeginWeek = $thisMonth - 1; $firstDayOfWeek = $firstDayOfWeek + date("t",mktime(0,0,0,$monthBeginWeek,1,date("Y"))); } else { $monthBeginWeek = $thisMonth; } $lastDayOfWeek = $todayDayOfMonth + (6 - $dayOfWeek); #Get the day of the month of the end of the week (sat) if($lastDayOfWeek > date("t")) #if the day of the month is larger then the number of days in the month { $monthEndWeek = $thisMonth + 1; $lastDayOfWeek = $lastDayOfWeek - date("t"); #Then take the number of days in the week to get the day number of the next week } else { $monthEndWeek = $thisMonth; } $start_of_week = date("Y-m-d",mktime(0,0,0,$monthBeginWeek,$firstDayOfWeek,date("Y"))); #echo $start_of_week; $end_of_week = date("Y-m-d",mktime(0,0,0,$monthEndWeek,$lastDayOfWeek,date("Y"))); #echo $end_of_week; ?> Any help is much appreciated! TIA This topic has been moved to Linux. http://www.phpfreaks.com/forums/index.php?topic=309899.0 Hi Guys, I'm new to PHP. I've gone through a few tutorials and I'm not sure how to go about the following: For work, every week 1 of 3 people is responsible for checking some bug report. This week I have to do this. Next week it's Tom the week after that is Brenda. Then it's back to me again and it cycles. We currently have a white board calendar at the back of the office that gets updated with the name of the person responsible for doing the checks that week. What I'm trying to do is write a small PHP snippet that will show on our internal site something like: Bug Checker this week is: TARA Then the next week it'll show Tom, then Brenda. And cycle back. That way we don't have to keep updating that board. Does anybody know the best way to go about this. I keep thinking of different ideas but they all seem pretty convoluted and I'm fairly certain there's got to be a very easy way to do it; it's just not coming to me. Any help would be appreciated Thanks so much! XXX Tara EDIT: We cycle through every Monday. If that helps. I considered creating a database file with the date and the person and just read that in but that is so embarassingly inefficient that I didn't even want to mention this in the post. I have a PHP script that downloads a file from a remote server but in order to download the correct file it has to subtract 1 day of the week. It used to work quite some time ago so I dont know what is going on now. The section of the script goes like this....
$days = array("sun","mon","tue","wed","thu","fri","sat"); $today = date("w"); $yesterday = $days[$today - 1];Then it joins the 3 letter version of the day of week with a file name to grab the correct file download. $yesterday is not returning the correct value instead I am getting an error... # ./update.php PHP Notice: Undefined offset: -1 in /root/fcc/work/update.php on line 6 fetching l_am_.zip ncftpget: server said: l_am_.zip: No such file or directory. PHP Warning: unlink(counts): No such file or directory in /root/fcc/work/update.php on line 23 data set empty nothing to process Sun, 01 February, 2015 6:29:06 PMSo in the above error I can see that $yesterday is "Undefined offset" and because of that the file it tries to download is incorrect, it should have tried to download l_am_sat.zip and it left out the 3 letter date code. Edited by chadrt, 01 February 2015 - 09:48 PM. Hi guys! This will seem complex but I really need your help If a have a variable with a date value (i.e. $a = '23/10/2011') How do you work out the week number of that date? Thanks This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=329323.0 I want to have the timestamps of week numbers, this is working perfectly from the year 2011 to 2012. It refuses to switch to the year 2013. Do anyone know what i'm doing wrong?: <?php $year='2012'; $weekno='01'; $date = strtotime($year.'W'.$weekno); for($i=0;$i<55;$i++){ print date('d-m-Y W',$date)."\n"; $date=(weekno($date,'1'))."\n"; $date=(int)$date; } function weekno($date,$step=''){ $year=date('Y',$date); $weekno=date('W',$date); $date = strtotime($year.'W'.$weekno); if($step>0){ $date = strtotime($year.'W'.$weekno. '+1 week'); } if($step<0){ $date = strtotime($year.'W'.$weekno. '-1 week'); } return($date); } ?> Hello, I am writing a CMS for a sports organization league. One of my pages grabs values from the database using only the current week. What I want to do is when the admin enters in a game date, I want it to automatically find the week number for the date they entered. I haven't been able to find anything online that works for me. Any help would be great! Thanks Hello. I have made a very simple calendar with the help of a tutorial. http://www.codewalkers.com/c/a/Date-Time-Code/Simple-PHP-Calendar/ Now the problem is, that the week start with Sunday, insted of Monday. The table is looks like this (See attached image) The code is: <?php date_default_timezone_set('Europe/Helsinki'); //This gets today's date $date =time () ; //This puts the day, month, and year in seperate variables $day = date('d', $date) ; $month = date('m', $date) ; $year = date('Y', $date) ; //Here we generate the first day of the month $first_day = mktime(0,0,0,$month, 1, $year) ; //This gets us the month name $title = date('F', $first_day) ; //Here we find out what day of the week the first day of the month falls on $day_of_week = date('D', $first_day) ; //Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero switch($day_of_week){ case "Sun": $blank = 0; break; case "Mon": $blank = 1; break; case "Tue": $blank = 2; break; case "Wed": $blank = 3; break; case "Thu": $blank = 4; break; case "Fri": $blank = 5; break; case "Sat": $blank = 6; break; } //We then determine how many days are in the current month $days_in_month = cal_days_in_month(0, $month, $year) ; //Here we start building the table heads echo "<table border=1 width=294>"; echo "<tr><th colspan=7> $title $year </th></tr>"; echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>"; //This counts the days in the week, up to 7 $day_count = 1; echo "<tr>"; //first we take care of those blank days while ( $blank > 0 ) { echo "<td></td>"; $blank = $blank-1; $day_count++; } //sets the first day of the month to 1 $day_num = 1; //count up the days, untill we've done all of them in the month while ( $day_num <= $days_in_month ) { echo "<td> $day_num </td>"; $day_num++; $day_count++; //Make sure we start a new row every week if ($day_count > 7) { echo "</tr><tr>"; $day_count = 1; } } //Finaly we finish out the table with some blank details if needed while ( $day_count >1 && $day_count <=7 ) { echo "<td> </td>"; $day_count++; } echo "</tr></table>"; ?> So what I want is that the week starts from Monday, not Sunday.
The broadcast industry (radio and TV) has a special calendar. The week starts on Monday. Week #1 is ALWAYS the week with January 1st. So I was wondering if anyone could come up with a good way of having a drop down box list the dates of the previous week starting from Saturday. The dates would need to go from Saturday to the coming/current friday. Or would I just be better off having it list the days 'Saturday' to 'Friday'. Any thoughts? Hi, I have a select box that has every week day as an option. When a user picks "Monday" and submits the form I want to echo the date of monday last week. And if he picks tuesday, I want it to give the date of Tuesday last week, on so on. How would I do that? Thank you in advance! I've got a table with 5 columns that shows appointments for Monday through Friday, but I'm using 5 separate queries, one for each column, for each corresponding day because i cannot figure out how to query the whole week and only put the appointments in the column they belong. Thank you for any help. I need to know which day of week was on particular date, so for example I would provide date lets say "Nov 22, 2010" formatted as needed and the function would return "Monday", is there a function in php to do that or how can it be done? I have the week date range displaying the week range from sunday - saturday
$current_dayname = date("0"); // return sunday monday tuesday etc. echo $date = date("Y-m-d",strtotime('last sunday')).'to'.date("Y-m-d",strtotime("next saturday")); Which outputs in the following format 2015-01-25to2015-01-31 However, when I press next or previous, the dates don't change. <?php $year = (isset($_GET['year'])) ? $_GET['year'] : date("Y"); $week = (isset($_GET['week'])) ? $_GET['week'] : date('W'); if($week > 52) { $year++; $week = 1; } elseif($week < 1) { $year--; $week = 52; } ?> <a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week == 52 ? 1 : 1 + $week).'&year='.($week == 52 ? 1 + $year : $year); ?>">Next Week</a> <!--Next week--> <a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week == 1 ? 52 : $week -1).'&year='.($week == 1 ? $year - 1 : $year); ?>">Pre Week</a> <!--Previous week--> <table border="1px"> <tr> <td>user</td> <?php if($week < 10) { $week = '0'. $week; } for($day= 1; $day <= 7; $day++) { $d = strtotime($year ."W". $week . $day); echo "<td>". date('l', $d) ."<br>". date('d M', $d) ."</td>"; } ?> </tr> </table> |