Change week dynamic in PHP for listview - php

I want to create a list where a week is displayed and the values of each day are displayed in the section of the day.
First I tried to create the dynamic week change so that you can press a button to show the week before or the next week.
Now I have this code and in the output I have a week from Sunday to Saturday.
Now here is my problem, since I always have Sunday as the first day the week that is output is also always the one from Sunday, so the week before the actual week.
I tried to change the week output in the variable $dow with date('N') from date('n'). Unfortunately this did not work.
How do I have to change my code to show / output the correct week from Monday to Sunday?
Note: under $ts += 0 * 86400 * 7; on the 0 you can change the week starting from the current week.
$ts = date(strtotime('last monday'));
$ts += 0 * 86400 * 7;
$dow = date('N' , $ts);
$offset = $dow;
//the output is currently only for testing
$ts = $ts - $offset * 86400;
$week = date('W', $ts);
echo "<p>$week</p>";
for ($x=0 ; $x<7 ; $x++,$ts += 86400) {
echo '<p>' . date("d.m.Y", $ts) . '</p>' ;
}
Output to the code and current week

Try something simpler, like this?
<?php
$currentDate = date('d.m.Y', strtotime('last monday'));
for ($x=0 ; $x<7 ; $x++) {
echo '<p>' . $currentDate . '</p>' ;
$currentDate = date("d.m.Y", strtotime($currentDate . ' +1 day'));
}

Related

Output current week number within month PHP

I have a PHP script to identify the current month and current week within that month.
For today, it should output 501 (i.e. 5th week of the 1st month), however right now it outputs 101.
Can someone explain why this is the case and how I can resolve? I feel I'm missing something obvious.
<?php
date_default_timezone_set("Europe/London");
function weekOfMonthFunction($date) {
$firstOfMonth = date("Y-m-01", strtotime($date));
return intval(date("W", strtotime($date))) - intval(date("W", strtotime($firstOfMonth)));
}
$week = weekOfMonthFunction($date2);
$week = $week + 1;
$month = date("m");
$week_month = $week.$month;
echo $week_month;
<?php
date_default_timezone_set("Europe/London");
function weekOfMonth() {
$now = time();
$week = date('W', $now);
$firstWeekOfMonth = date('W', strtotime(date('Y-m-01', $now)));
return 1 + ($week < $firstWeekOfMonth ? $week : $week - $firstWeekOfMonth);
}
$week = weekOfMonth();
$week_month = $week . date("m");
echo $week_month;
501
Try it online!
You've fixed the date to Y-m-01 instead of Y-m-d from the parameter date.
How about this alternative?
function weekOfMonthFunction($date) {
list($d, $m) = explode('-', date("d-m", strtotime($date)));
return intval(ceil($d/7).str_pad($m, 1, '0', STR_PAD_LEFT));
}
echo weekOfMonthFunction('2021-01-25'); //Give 401
echo weekOfMonthFunction('2020-01-29'); //Gives 501
Explanation:
Date of month divided by 7 rounded up to get week number prepended to month number with a padded 0

1st I converted date to week number ( 01 to 53 ) in PHP and now i don't know how to get week number to first_date of week

Here a simple code of
date to week_number of year and now. How can I get start_date and end_date of week_number
$date_string = "2012-12-30";
echo "Weeknummer: " . date("W", strtotime($date_string));
There is many ways to do it, here is one.
I use "N" of date to determine what day the selected day is and use strtotimes way of understanding simple text to find previous or next Monday/Sunday.
$date_string = "2018-01-27";
$w =date("W", strtotime($date_string));
$N =date("N", strtotime($date_string));
If($N == 1){ // if monday
$monday = $date_string;
$sunday = date("Y-m-d", strtotime("next Sunday $date_string"));
}Elseif($N ==7){ // if sunday
$monday = date("Y-m-d", strtotime("previous Monday $date_string"));
$sunday = $date_string;
}Else{// any other weekday
$monday = date("Y-m-d", strtotime("previous Monday $date_string"));
$sunday = date("Y-m-d", strtotime("next Sunday $date_string"));
}
echo "Weeknummer: $w.\nMonday: $monday.\nSunday: $sunday.";
Output:
Weeknummer: 52.
Monday: 2012-12-24.
Sunday: 2012-12-30.
https://3v4l.org/UDoqF
You can use this:
<?php
$date_string = "2012-12-30";
$weekNumber = date("W", strtotime($date_string));
echo "Weeknummer: ".$weekNumber;
echo '</br>';
// get the year
$year = date("Y", strtotime($date_string));
// set the date string for the week number
$dateWeek = $year.'-W'.$weekNumber;
// increase the weekNumber to the next
$weekNumber = intval($weekNumber);
$weekNumber += 1;
// if it is lower than 10 add preceeding 0
if($weekNumber < 10) $weekNumber = '0'.$weekNumber;
// set the date string for the next week number
$dateWeekNext = $year.'-W'.$weekNumber;
echo '</br>';
// get the first day of the week
echo date('Y-m-d', strtotime($dateWeek));
echo '</br>';
// get the day before the first day of the next week
echo date('Y-m-d', strtotime($dateWeekNext . ' -1 day'));
?>
This outputs:
Weeknummer: 52
2012-12-24
2012-12-30

PHP date calculate date between two day

I want to get number of weekends and number of bussiness day by I know only $startDate and $endDate, Is have any PHP's function that can calculate automatic ?
This is my code :
$endDate = strtotime($endDate);
$startDate = strtotime($startDate);
echo $days = ($endDate - $startDate) / 86400 + 1;
My code will return how many day, and how I need more is what day ?
How I can get it?
Example:
startDate:`2014-11-17`
endDate:`2014-11-19`
I want this Output:
it's 3 day
2014-11-17 is Monday
2014-11-18 is Tueday
2014-11-19 is Wendnesday
How about...
for ($time = $startDate; $time <= $endDate; $time += 86400) {
echo date('Y-m-d \i\s l', $time) . '<br>';
}
This might help you, or at least may give you some clue
$endDate = strtotime($endDate);
$startDate = strtotime($startDate);
echo $days = ($endDate - $startDate) / 86400 + 1;
$time = $startDate;
while ($time <= $endDate) {
echo date('Y-m-d', $time) . ' is ' . date('l', $time);
$time += 86400;
}
if ($time != $endDate) {
echo date('Y-m-d', $time) . ' is ' . date('l', $time);
}
This should work for you:
<?php
$startDate = strtotime("2014-11-17");
$endDate = strtotime("2014-11-19");
echo "It's " . $days = ($endDate - $startDate) / 86400 + 1 . " days";
for($count = 0; $count < $days; $count++)
echo "<br />" . date('Y-m-d', strtotime('+' . $count . ' day', $startDate)) . ' is ' . date('l', strtotime('+' . $count . ' day', $startDate));
?>
Output:
It's 3 days
2014-11-17 is Monday
2014-11-18 is Tuesday
2014-11-19 is Wednesday
There's no built-in. Excel has NETWORKDAYS(), but in PHP you must roll your own.
There is a PHP solution at Day difference without weekends
Note its limitations in the comments there e.g. if a public holiday falls on a weekend day.
The following page describes an alternative implementation of NETWORKDAYS(): http://www.cpearson.com/excel/betternetworkdays.aspx
It's not PHP but it demonstrates the logic.
Unfortunately there is no shortcut other than looping through each day in the period and deciding whether or not to count it. You need to accept arguments for (or hard-code) the weekend days and the dates of any public holidays (if you are excluding public holidays).
If you are doing this frequently and for long periods, you might pre-compute and cache the number of business days for each calendar month and year; then, at run-time, you look up the number of days in each whole year within the period, then the remaining whole months, then do the ordinary loop for the remaining days at the start and end of the period.
EDIT: If you just want to exclude weekends (not public holidays), then you can calculate 5 days for each whole week in the period, and then calculate any remaining days: https://github.com/bgarlock/scripts/blob/master/PHP%20Equivalent%20of%20MS%20Excel%20NETWORKDAYS%20function.php
Please find below code I am sure it works for you
NOTE : Parameter passed P1D is denoting 1 Day difference same way you can pass P1M for 1 month P1W for 1 week and P1Y for 1 Year.
$date1 = '29/08/2013';
$date2 = '03/09/2013';
function returnDates($fromdate, $todate) {
$fromdate = \DateTime::createFromFormat('d/m/Y', $fromdate);
$todate = \DateTime::createFromFormat('d/m/Y', $todate);
return new \DatePeriod(
$fromdate,
new \DateInterval('P1D'),
$todate->modify('+1 day')
);
}
$datePeriod = returnDates($date1, $date2);
foreach($datePeriod as $date) {
echo $date->format('d/m/Y'), PHP_EOL; //you can set any date format here
}

Dummy php date compare

I'm dummy and dont know how to compare date's.
I need to do link unclickable when date is less or equal today.
This is actullay not complete code, full code prints this week monday to sunday. Im try to do calendar what print 1 week each time and each day it print time 9am to 17pm.
$toDay = date("m-d-y");
$first = date('m-d-Y', strtotime('Last Monday +'.$d.' days'));
//This sunday + 1 week
$last = date('m-d-Y', strtotime('Next Sunday +'.$d.' days'));
//print week number
**if($first <= $toDay() && $tomorrow <= $toDay)**
{
echo"<p class='list_header'>". $tomorrow."</p>";
//Looping time 9 to 17
for($time_start = 9; $time_start <= 17; $time_start+=2)
{
echo "<li style='background-color:red'><a href='#'>".$time_start."</a></li>";
}
}
You can use timestamp method to compare the time value in php.

PHP get date after one week and calculate the number of days left

I have a dynamic date, now what i want is that finding the date after exact one week, i have achieved that with the code below, but now i want that now many days are left for that week after date to come. i have got some sort of time stamp, but i don't know how to convert it to DAYS LEFT.
$weekDate = date( "d/m/Y", strtotime("19-05-2014") + 86400 * 7 );
echo $weekDate;// THATS PERFECT
////////////////////////////////////////////////////////////////
$future = strtotime( $weekDate ); //Future date.
$datediff = time() - $future;
$days = floor( ( ( $datediff / 24 ) / 60 ) / 60 ); //this is not perfect, returns some
sort of timestamp
I have tried other methods which are fine, but if week completes on 26, and today is 25th it gives me 0 days left, but it should say 1 day left. please help me.
In your $date_diff now is less than the future date thats why its zero. Inside strtotime() function, you can directly put a relative date inside. In this case, for one week you can use +1 week or +7 days. Consider this example:
$next_week = date('d/m/Y', strtotime('19-05-2014 +1 week')); // 26/05/2014
$next_week = strtotime('19-05-2014 +7 days');
$difference = $next_week - time(); // next weeks date minus todays date
$difference = date('j', $difference);
echo $difference . (($difference > 1) ? ' days ' : ' day ') . ' left';
// should output: 1 day left
Alright. I did something. Here's the code
$startDate = strtotime("19-05-2014");
$endDate = $startDate + 604800;
$diff = ($endDate - time()) / 60 / 60 / 24;
if ($diff < 1 && $diff > 0) {
$days = 1;
} else {
$days = floor($diff);
}
echo $days;
The problem you have with getting "1 day" if the date is tomorrow is the floor method. strtotime() gives you the time at 0 a.m. if you don't set it by your own. Because of that the difference between now and tomorrow is less than 1 which is 0 if you floor that. I created an if-clause for that.
But that will give you "1 day" for today and "1 day" for yesterday (last 2 days before the final date). If you want that better, you have to specify time in your initial date (19-05-2014).
Use DateTime for date and time calculations.
$weekDate = new \DateTime('+ 1 week');
$future = new \DateTime('+ 3 days');
$daysLeft = $weekDate->diff($future)->days;
echo $daysLeft; //4
See it working.
Reference http://php.net/datetime

Categories