I want to make a calendar based on weeks with php found this script. If i get week 18 the first day is in april. How can i make it wright &bnsp; for the first day?
If you look at Juni there is 4 blank days in the begining of the week then i need php to print 4 &bsp; (blank day) hope you can help.
$week_number = 18;
$year = 2018;
if($week_number < 10){
$week_number = "0".$week_number;
}
for($day=1; $day<=7; $day++)
{
echo date('d', strtotime($year."W".$week_number.$day))."\n";
}
This will do what you want.
I save the days in an array instead and then find the key of the lowest number (that's the start of next month).
Then I loop the array and if the key is less than the start position I echo new line, else the value.
for($day=1; $day<=7; $day++){
$days[$day] = date('d', strtotime($year."W".$week_number.$day))."\n";
}
$start = array_keys($days, min($days))[0];
for($day=1; $day<=7; $day++){
If($day<$start){
Echo "\n";
}Else{
Echo $days[$day];
}
}
https://3v4l.org/dIhYZ
Ok works good to hard for me to do myself now, but i will lern. That works for the start of the week but how it will look at the end of the week? The 31st is on a thursday.
Related
My mind was blown right now.. I couldn't figure out why the next month of January become March. I hope anyone in here can help me and give me explanation why it happened.
I am trying to customize date by next month with its cutoff day where next day of 05 is 20 and 15 is 30
Here is the code
$cutoff = "2021-01-30";
$datetime = new DateTime($cutoff);
echo $datetime->format('Y-m-d')."<br>";
$day = substr($cutoff, 8);
if($day == "05")
{
$next_cutoff = $datetime->format('Y-m-20');
}
elseif($day == "15")
{
$datetime->modify('last day of this month');
$next_cutoff = (substr($datetime->format('Y-m-d'), 8) <= 30) ? $datetime->format('Y-m-d') :
$datetime->format('Y-m-30');
}
else
{
$datetime->modify('next month');
$next_cutoff = ($day == "20") ? $datetime->format('Y-m-05') : $datetime->format('Y-m-15');
}
echo $next_cutoff;
I tried to set the $cutoff variable to $cutoff = "2021-01-28" and it went fine
But, when I tried to set it to $cutoff = "2021-01-29" and up that's the time the next month became March
I tried, other month also and it works fine. Just this one is the only problem. Is there anyone can help me out there?
Try to change "next month" to "first day of next month" in your code:
// $datetime->modify('next month');
$datetime->modify('first day of next month');
Spent a while searching and couldn't find any answers. Really simple question I hope.
In my database I have a string column dayOfWeek.
I use this to track what day of week weekly events are scheduled for. Usually I match the dayOfWeek to the current day using PHP and display the correct events.
What I want to do is show a calendar into the future which makes a list of the future dates of the weekly recurring event.
For example, if the event takes place on Tuesday, and Feb 2017 has Tuesdays on the 7th, 14th, 21st, and 28th. I'd like to use PHP to display as such.
I know strtotime() is used to do the opposite, find a day from the date, but can the opposite be done easily?
Sorry if my question is poorly worded or missing information.
USE CASE FOR FEB 2017 TUESDAYS
$n=date('t',strtotime("2017-02-01")); // find no of days in this month
$dates=array();
for ($i=1;$i<=$n;$i++){
$day=date("D",strtotime("2017-02-".$i)); //find weekdays
if($day=="Tue"){
$dates[]="2017-02-".$i;
}
}
print_r($dates);
http://phpfiddle.org/main/code/s8wq-xx44
You could do this:
// The day of the week.
$dayOfEvent = row['day'];
// Say, you wanted the next 5 weeks.
$numberOfWeeks = 5;
$tempDate = date(U);
$counter = 0;
do{
$tempDate = $tempDate + 86,164;
if(date('D', $tempDay) == 'Tue'){
echo date('l jS \of F Y', $tempDay);
$counter++;
}
}while($counter < $numberOfWeeks);
First try to find the date for the day in current week. After that keep adding 7 days multiples to it.
<?php
// $timestamp = now();
for ($i=0; $i < 7; $i++) {
$days = $i." days";
$t = date('Y-m-d', strtotime($days));
$dayOfTheDay = date("D",strtotime($t));
if ( $dayOfTheDay == day from table )
{
$current_date = $t;
break;
}
}
for ($i=0; $i < 200; $i++) {
in this loop add 7 days to current_date
}
?>
I can display dates from start to end date from stored data in mysql, but I want to display current month dates from 1st to end date of this month in form of
1
2
3
4
.
.
.
.
.
31
Is this possible?
Refer to PHP cal_days_in_month
As explained here
This function will return the number of days in the month of year for the specified calendar.
int cal_days_in_month ( int $calendar , int $month , int $year )
And an example:
$number = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
echo "There were {$number} days in August 2003";
Use a loop to display a count of the number of days
For the PHP part, this might help you:
// Get the current date
$today = getdate();
// Get the number of days in current month
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $today['mon'], $today['year']);
// Print the dates
for ($i = 1; $i <= $days_in_month; $i++) {
echo ' ' . $i;
}
Styling and output is another task, this is just to get you started.
yes. it is possible.
please, use below php code. it can work for php 4.1 and higher.
<?php
$number = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
for($i=1;$i<=$number;$i++)
echo $i.'<br>';
?>
If you want all days in the month, try this loop where date("t") give you the numerical last day of the month, and we know the first day is always 1.
$last = date("t");
for($i=1; $i<= $last; $i++) echo "$i ";
How would I get the nearest of two dates once I've defined them both as variables?
Here's the code to calculate the next Monday and Friday:
//calculates nearest Monday
$nextMonday = strtotime("next Monday");
$mondayParade = date("d/m/Y", $nextMonday);
//calculates nearest Friday
$nextFriday = strtotime("next Friday");
$fridayParade = date("d/m/Y", $nextFriday);
When echoing $mondayParade it displays 26/01/2015 which is correct. $fridayParade also works as above but shows friday's date.
I'd like to be able to calculate which of these two dates is closest to my current date.
I read about also using strtotime for this but can't figure out how.
Thanks
$now = time();
$nextMondayDiff = abs($now - $nextMonday);
$nextFridayDiff = abs($now - $nextFriday);
if ($nextMondayDiff < $nextFridayDiff) {
echo 'Monday is closer';
} else {
echo 'Friday is closer';
}
Or as #David points out in the comments, assuming both dates are always guaranteed to be in the future:
if ($nextMonday < $nextFriday) {
echo 'Monday is closer';
} else {
echo 'Friday is closer';
}
if($nextMonday < $nextFriday) {
echo "Monday";
} else {
echo "Friday";
}
I searched the millions of similar posts about this but cannot find how to make it count down from 31, 30, 29, 28 etc.
I can have the previous calendar blocks show the 31st but that's all. I need it to show previous month 31st, 30, 29, etc.
updated code from Renku:
//define the variable dayCol
$dayCol = 0;
// Print last months' days spots.
for ($i=0; $i<$leadInDays; $i++) {
$lastmonth = date('d', strtotime(-$i.' day', strtotime($startDate))); // Days in previous month
print "<td width=\"14%\" height=\"25%\" class=\"calendar_cell_disabled_middle\">$lastmonth</td>\n ";
$dayCol++;
}
example :
I am writing a new loop for this.
<?php
$StartDate= date("Y-F-d",strtotime("+0 Month"));// get first day of current month
$num= 10; // how many past days you need from previous month + 1 (to remove current day)
for ($i=1; $i<$num; $i++) {
echo $prev= date('Y-m-d', strtotime(-$i.' day', strtotime($StartDate)))."<br />"; //get last days of previous month
}
?>
I am re writing it with your loop,
<?php
$dayCol = 0;
$leadInDays = 5; // (just for February cuz theres 5 blanks before its the 1st of Feb)
$StartDate= date("Y-F-d",strtotime("+0 Month"));
// Print last months' days spots.
for ($i=1; $i<($leadInDays+1); $i++) {
$lastmonth = date('d', strtotime(-$i.' day', strtotime($StartDate))); // Days in previous month
print "<td width=\"14%\" height=\"25%\" class=\"calendar_cell_disabled_middle\">$lastmonth</td>\n ";
$dayCol++;
}
?>
Test it Here
I would use date('t') to get the number of days in said month and just loop backwards:
$month = '2013-02-05';
for($i = date('t', strtotime($month)); $i > 0; $i--) {
...
}