Get 3 future day dates with php - php

I want to creat a table for listing 3 future days with day name and date for my online shopping.
I try this code but it doesn't work correctly :
function week_from_monday($date) {
// Assuming $date is in format DD-MM-YYYY
list($day, $month, $year) = explode("-", $_REQUEST["date"]);
// Get the weekday of the given date
$wkday = date('l',mktime('0','0','0', $month, $day, $year));
switch($wkday) {
case 'Monday': $numDaysToMon = 0; break;
case 'Tuesday': $numDaysToMon = 1; break;
case 'Wednesday': $numDaysToMon = 2; break;
case 'Thursday': $numDaysToMon = 3; break;
case 'Friday': $numDaysToMon = 4; break;
case 'Saturday': $numDaysToMon = 5; break;
case 'Sunday': $numDaysToMon = 6; break;
}
// Timestamp of the monday for that week
$monday = mktime('0','0','0', $month, $day-$numDaysToMon, $year);
$seconds_in_a_day = 86400;
// Get date for 7 days from Monday (inclusive)
for($i=0; $i<7; $i++)
{
$dates[$i] = date('Y-m-d',$monday+($seconds_in_a_day*$i));
}
return $dates;
}
$ddate = date('Y-m-d');
$date = new DateTime($ddate);
$week = $date->format("W");
$week_number = $week;
$year = date('Y');
for($day=5; $day<=30; $day++)
{
echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n";
}
Here is my code but I want to get this :
today : Sun
Monday 30
Tuesday 31
Wednesday 1

To get the next 3 upcoming dates, you can simplify this a lot using date and strotime.
Try this code:
function nextThreeDays($date) {
$dateTs = strtotime($date);
// Dont need this, but I leave it for educational purpose:
// $day_of_week = date("N", $dateTs) - 1; // Mon=0 Tue=1 Wed=2 ...
// $monday_time = strtotime(date("d.m.Y H:i:s", $dateTs) . " -".$day_of_week." days"); // timestamp of monday of week of $date
for($i = 1; $i <= 3; $i++) {
echo date('m/d/Y', strtotime(date("d.m.Y H:i:s", $dateTs) . " +".$i." days"))."\n";
}
}
// Put in "today : Sun"
nextThreeDays("29.01.2023");
Outputs:
// Monday 30 Tuesday 31 Wednesday 1
01/30/2023
01/31/2023
02/01/2023

Related

PHP : it is not working fine for Feb 2022. it only gives 4 weeks limited upto 27 feb while 28feb is remaining as fifth week day. please resolve it

$month = '2';
$year = '2022';
$lastDayOfWeek = '7'; //1 (for monday) to 7 (for sunday)
function getWeeksInMonth($year, $month, $lastDayOfWeek)
{
$aWeeksOfMonth = [];
$date = new DateTime("{$year}-{$month}-01");
$iDaysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$aOneWeek = [$date->format('Y-m-d')];
$weekNumber = 1;
for ($i = 1; $i <= $iDaysInMonth; $i++)
{
if ($lastDayOfWeek == $date->format('N') || $i == $iDaysInMonth)
{
$aOneWeek[] = $date->format('Y-m-d');
$aWeeksOfMonth[$weekNumber++] = $aOneWeek;
$date->add(new DateInterval('P1D'));
$aOneWeek = [$date->format('Y-m-d')];
$i++;
}
$date->add(new DateInterval('P1D'));
}
return $aWeeksOfMonth;
}
$weeks = getWeeksInMonth($year, $month, $lastDayOfWeek);
foreach($weeks as $weekNumber => $week){
echo "Week {$weekNumber}: {$week[0]} - {$week[1]}";
echo '<br>';
}
OUTPUT
Week 1: 2022-02-01 - 2022-02-06
Week 2: 2022-02-07 - 2022-02-13
Week 3: 2022-02-14 - 2022-02-20
Week 4: 2022-02-21 - 2022-02-27

php timeout on for loop with dates

I want to get an array of dates within a period. To do so I thought up a for loop (seems simple enough...) But when I run it even with for the dates of 1 month it times out.
This is my php:
$startdate = '2018-01-31';
$recurring = '2';
switch($recurring) {
case '1':
$period = '+1 day';
break;
case '2':
$period = '+1 week';
break;
case '3':
$period = '+1 month';
break;
case '4':
$period = '+3 months';
break;
case '5':
$perion = '+1 year';
break;
default:
$period = null;
break;
}
$dates = [];
if($period !== null) {
for($date = $startdate; $date < strtotime('+1 month', $startdate); strtotime($period, $date)) {
$dates[] = $date;
}
}
echo json_encode($dates);
Increasing $date with $date = strtotime($period, $date) in the increment part of the for loop should keep it from timing out, but there are a couple of other improvements that could be made.
First I'd recommend calculating your end date once before the loop to avoid extra strtotime calls each time it checks the continuation condition.
$end = strtotime("$startdate +1 month");
Then, set $date = strtotime($startdate) in the initialization part, or you'll get a date string instead of a timestamp as the first value in your $dates array.
for ($date = strtotime($startdate); $date < $end; $date = strtotime($period, $date)) {
$dates[] = $date;
}

Function for selecting next Day If the last day is weekend

I am trying to create a function that checks the range of date. If the last date is an weekend it will select the next day. Suppose, Today is 1 March 2016. If someone add 5 with it, it will make the result 7 March 2016. Because 6 March is Weekend. I have written this code. But it is not working and I cannot find where is the problem. The code is as below:
<?php
$date = "2016-02-29";
$numOfDays = 8;
$futureDate = strtolower(date("l",strtotime($date." +".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";
if($futureDate != $$w1 || $futureDate != $w2){
$finalDate = date("Y-m-d",strtotime($futureDate));
}
else{$finalDate = date("Y-m-d",strtotime($futureDate ."+1 day"));}
echo $finalDate;
?>
Check this:
<?php
$date = "2016-02-29";
$numOfDays = 11;
$futureDate = strtolower(date("l",strtotime($date ."+".$numOfDays."days")));
$futureDate1 = strtolower(date("Y-m-d",strtotime($date ."+".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";
if($futureDate == $weekend1){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+2 days"));
}
if ($futureDate == $weekend2){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+1 days"));
}
echo $finalDate1;
?>
March 6th, 2016 is a Sunday, but you're checking for Friday and Saturday, you need to check for Saturday and Sunday instead, for the weekend. Also, your variable names need to match, and for Saturday you'll need to add two days to get the next weekday.
<?php
$date = "2016-02-29";
$numOfDays = 8;
$day = 86400;//one day is 86400 seconds
$time = strtotime($futureDate + $numOfDays * $day);//converts $futureDate to a UNIX timestamp
$futureDate = strtolower(date("l", $time));
$saturday = "saturday";
$sunday = "sunday";
if($futureDate == $saturday){
$finalDate = date("Y-m-d", $time + 2 * $day);
}
else if($futureDate == $sunday){
$finalDate = date("Y-m-d", $time + $day);
}
else{
$finalDate = date("Y-m-d", $time);
}
echo($finalDate);
?>
#Fakhruddin Ujjainwala
I have changed in your code and it works now perfect. Thanks. the code is now as below:
<?php
$date = "2016-02-29";
$numOfDays = 4;
$futureDate = strtolower(date("l",strtotime($date ."+".$numOfDays."days")));
$futureDate1 = strtolower(date("Y-m-d",strtotime($date ."+".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";
if($futureDate == $weekend1){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+2 days"));
}
else if ($futureDate == $weekend2){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+1 days"));
}
else{
$finalDate1 = date("Y-m-d",strtotime($futureDate1));
}
echo $finalDate1;
?>

PHP DateInterval. Go back to the beginning of the year

I would like to write a method which I can give a period of time (Like: yearly, monthly...) and it returns me an anterior date according to this period of time given.
Here is my code:
public function callRuleCeilling($period)
{
$start = new \DateTime();
switch ($period) {
case 'weekly':
$dateInterval = 'P7D';
break;
case 'monthly':
$dateInterval = 'P1M';
break;
case 'quaterly':
$dateInterval = 'P3M';
break;
case 'half-yearly':
$dateInterval = 'P6M';
break;
case 'yearly':
$dateInterval = 'P1Y';
break;
default:
$dateInterval = 'P1Y';
}
$start->sub(new \DateInterval($dateInterval));
return $start
}
My example problem:
If I put a starting date in the middle of the year with a yearly period. I want it to stop at the beginning of the year.
And I would like the same for monthly period (Stop at the beginning of the month) etc...
Does it exist a PHP function with do that? I can't find it.
Please highlight me.
Thanks fo the highlight. It allowed me to did that way:
public function callRuleCeilling($period)
{
$start = new \DateTime();
$month = 'January';
switch ($period) {
case 'weekly':
$timestampMonday = strtotime('last monday', strtotime('tomorrow'));
$start = $start->setTimestamp($timestampMonday);
break;
case 'monthly':
$month = $start->format('F');
$start = new \DateTime('first day of '.$month);
break;
case 'quaterly':
$monthNumber = $start->format('n');
if($monthNumber >= 1) $month = 'January';
if($monthNumber >= 5) $month = 'May';
if($monthNumber >= 9) $month = 'September';
$start = new \DateTime('first day of '.$month);
break;
case 'half-yearly':
$monthNumber = $start->format('n');
if($monthNumber >= 1) $month = 'January';
if($monthNumber >= 7) $month = 'July';
$start = new \DateTime('first day of '.$month);
break;
case 'yearly':
$start = new \DateTime('first day of January');
break;
default:
$start = new \DateTime('first day of January');
}
return $start;
}

PHP Beginner...Simple PHP syntax error

I'm just getting started on my PHP journey and was completing a tutorial for the creation of a simple calendar. I'm running into a syntax error in Codepad and haven't been able to find a fix. I'm sure it's something simple that I'm not seeing. Sorry about the notes, I've been trying to annotate as much as possible so I don't get lost.
The error is:
Parse error: syntax error, unexpected ',' on line 10. (the $day=('d', $date) declaration)
Code:
<?php
// current date variable
$date = time ();
//day, month and year variables
$day = ('d', $date);
$month = ('m', $date);
$year = ('Y', $date);
// first day of the month
$monthfirstday = mktime(0,0,0,$month, 1, $year);
// get the name of the month
$monthtitle = ('F', $monthfirstday);
// first day of the week
$weekday = ('D', $monthfirstday);
// identify the days of the week
switch ($weekday) {
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;
}
// number of days in the month
$daysinmonth = cal_days_in_month(0, $month, $year);
// include the html
echo "<div id='calendar-wrap'>";
echo "<table border=6 width=394><tr><th colspan=60> $monthtitle $year</th></tr>";
echo "
<tr>
\n\t\t<td width=62>SUN</td>
\n\t\t<td width=62>MON</td>
\n\t\t<td width=62>TUES</td>
\n\t\t<td width=62>WEDS</td>
\n\t\t<td width=62>THURS</td>
\n\t\t<td width=62>FRI</td>
\n\t\t<td width=62>SAT</td>
</tr>
";
$daycount = 1;
echo "<tr>";
// dealing with the days of the month
$blank > 0
{
echo "<td></td>";
$blank = $blank-1;
$daycount++;
}
// set the day number to 1
$daynumber = 1;
// count the days of the month
while
( $daynumber <= $daysinmonth )
{
echo "<td> $daynumber </td>";
// increase the day count until the month ends
$daynumber++;
$daycount++;
// add a new row every 7 days
if ($daycount > 7)
{
echo "</tr><tr>";
$daycount = 1;
}
}
// fill in blank days if necessary
while
($daycount > 1 && $daycount <= 7)
{
echo "<td> </td>";
$daycount++;
}
echo "</tr></table></div>";
?>
Thanks in advance,
Mike
Your function is missing!
$day = date('d', $date);
Here you go...
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
I think you're trying to do this:
$day = date('d', $date);
This is the Error. Missed the Function :)
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);

Categories