I am actually trying to make a monthly reward system with a "stamp" interface.
For that, I want to use an HTML Table with 1 cell = 1 day, starting Monday.
Here is my problem (and the solution is probably easy to find !) : Monday isn't the first day of month - not for all month, atleast. How can I ask strtotime() & date() to show the date Y-m-d for :
"monday of first week of month" ?
"sunday of last week of month" ?
I'm expecting result like this for 2019-08 :
2019-07-29
2019-09-01
I did not find it on PHP Documentation but is there a way to place a third argument on date() to set up the date ?
Like that, I could have done something like this :
date('Y-m-d', strtotime('monday'), '2019-08-01');
EDIT :
Based on #Vidal answer here is what I tried, but I got a strange PHP reaction :
$first_date = date("Y-m-d", strtotime("first monday 2019-08 - 7 days"));
$last_date = date("Y-m-d", strtotime("last sunday 2019-09"));
$last_date2 = date("Y-m-d", strtotime("last sunday 2019-09 + 7 days"));
echo $first_date; ?><br>
echo $last_date; ?><br>
echo $last_date2; ?><br>
Output :
2019-07-29
2019-08-25
2019-09-08
I don't know why. Did you also notice that I need to set $last_date & $last_date2 to 2019-09 instead of 2019-08 ?
I think it's better to work with the DateTime object.
/*
I'm expecting result like this for 2019-08 :
"monday of first week of month" => 2019-07-29
"sunday of last week of month" => 2019-09-01
*/
$month = "2019-08";
$date = date_create($month."-02")
->modify("last Monday")
->format('l, d-m-Y')
;
echo $date."<br>";
$date = date_create("last Day of ".$month)
->modify("-1 Day")
->modify("next Sunday")
->format('l, d-m-Y')
;
echo $date."<br>";
/* Output
Monday, 29-07-2019
Sunday, 01-09-2019
*/
Do a loop for year, month.
echo date("j, d-M-Y", strtotime("first monday of 2019-08"));
echo date("j, d-M-Y", strtotime("last sunday of 2019-08"));
Hope it helps.
PHP Reference
I managed to make it work by changing some things to your code:
1) If the first monday is > 2 and <= 7, you have to remove 7 days to actually get the first monday
2) I added the '+ 7 days' on another line of code. It didn't work if I had 'last sunday of XXXX-XX + 7 days'
3) To get the last sunday based on your desired outputs, I compared the last sunday of the month with the total number of days in the month - 7. If it's bigger than that (25 for example), it means that it's not the last sunday
$year = 2019;
$month = 8;
if($month < 10)
$month = "0" . $month;
$currentDate = $year . "-" . $month;
if(date("d", strtotime("first monday of " . $currentDate)) > 2 && date("d", strtotime("first monday of " . $currentDate)) <= 7)
$first_date = date("Y-m-d", strtotime("first monday of " . $currentDate . " -7 days"));
else
$first_date = date("Y-m-d", strtotime("first monday of " . $currentDate));
if(date("d",strtotime("last sunday of " . $currentDate)) > date('t') - 7)
{
$last_date = date("Y-m-d", strtotime("last sunday of " . $currentDate));
$last_date = date("Y-m-d",strtotime($last_date . " +7 days")); //working when you add days like that. doesn't work if I put + 7 days in the 'last sunday of'...
}
else
$last_date = date("Y-m-d", strtotime("last sunday of " . $currentDate));
echo $first_date . "<br>";
echo $last_date . "<br>";
Related
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
how can I get last week' date range in php ?
see my codes bellow:
<?php
function get_last_week_dates(){
// how can i get the date range last week ?
// ex: today is 2014-2-8
// the week date range of last week should be '2014-1-26 ~ 2014-2-1'
}
?>
You can use strtotime()
$previous_week = strtotime("-1 week +1 day");
$start_week = strtotime("last sunday midnight",$previous_week);
$end_week = strtotime("next saturday",$start_week);
$start_week = date("Y-m-d",$start_week);
$end_week = date("Y-m-d",$end_week);
echo $start_week.' '.$end_week ;
UPDATE
Changed the code to handle sunday. If the current day is sunday then - 1 week will be previous sunday and again getting previous sunday for that will go the one week back.
$previous_week = strtotime("-1 week +1 day");
In addition if we need to find the current week and next week date
range we can do as
Current week -
$d = strtotime("today");
$start_week = strtotime("last sunday midnight",$d);
$end_week = strtotime("next saturday",$d);
$start = date("Y-m-d",$start_week);
$end = date("Y-m-d",$end_week);
Next Week -
$d = strtotime("+1 week -1 day");
$start_week = strtotime("last sunday midnight",$d);
$end_week = strtotime("next saturday",$d);
$start = date("Y-m-d",$start_week);
$end = date("Y-m-d",$end_week);
Simply use
date("m/d/Y", strtotime("last week monday"));
date("m/d/Y", strtotime("last week sunday"));
It will give the date of Last week's Monday and Sunday.
you need you use strtotime function for this
<center>
<?php
function get_last_week_dates()
{
// how can i get the date range last week ?
// ex: today is 2014-2-8
// the week date range of last week should be '2014-1-26 ~ 2014-2-1'
$startdate = "last monday";
if (date('N') !== '1')
{
// it's not Monday today
$startdate .= " last week";
}
echo "<br />";
$day = strtotime($startdate);
echo date('r', $day);
echo "<br />";
$sunday = strtotime('next monday', $day) - 1;
echo date('r', $sunday);
}
get_last_week_dates();
?>
</center>
Well just for the fun of trying to solve this:
date_default_timezone_set('UTC');
$firstDayOfLastWeek = mktime(0,0,0,date("m"),date("d")-date("w")-7);
$lastDayOfLastWeek = mktime(0,0,0,date("m"),date("d")-date("w")-1);
echo("Last week began on: ".date("d.m.Y",$firstDayOfLastWeek));
echo("<br>");
echo("Last week ended on: ".date("d.m.Y",$lastDayOfLastWeek));
This should do the trick
$startWeek = date('Y-m-d',strtotime("Sunday Last Week"));
$endWeek = date('Y-m-d',strtotime("Sunday This Week"));
this would not work if ran on a Monday. It would get last Sunday (the day before) to the next Sunday. So using Abhik Chakraborty's method with the above:
$startTime = strtotime("last sunday midnight",$previous_week);
$endTime = strtotime("next sunday midnight",$startTime);
$startDate = date('Y-m-d 00:00:00',$startTime);
$endDate = date('Y-m-d 23:59:59',$endTime);
This will now give
start = 2014-08-10 00:00:00
endDate = 2014-08-17 23:59:59
I know this is old but here's a much more succinct way of doing it:
$startDate = date("m/d/y", strtotime(date("w") ? "2 sundays ago" : "last sunday"));
$endDate = date("m/d/y", strtotime("last saturday"));
echo $startDate . " - " . $endDate
This one will produce the proper result and handles the Monday issue
<?php
$monday = strtotime("last monday");
$monday = date('W', $monday)==date('W') ? $monday-7*86400 : $monday;
$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
$this_week_sd = date("Y-m-d",$monday);
$this_week_ed = date("Y-m-d",$sunday);
echo "Last week range from $this_week_sd to $this_week_ed ";
?>
Most of those other solutions offered were off by one day.
If you want Sunday to Saturday for last week, this is the way to do it.
$start = date("Y-m-d",strtotime("last sunday",strtotime("-1 week")));
$end = date("Y-m-d",strtotime("saturday",strtotime("-1 week")));
echo $start. " to ".$end;
Carbon
$startOfTheWeek = Carbon::now()->subWeek(1)->startOfWeek();
$endOfTheWeek = Carbon::now()->subWeek(1)->endOfWeek();
From a specific date
$startOfTheWeek = Carbon::parse('2020-03-02')->subWeek(1)->startOfWeek();
$endOfTheWeek = Carbon::parse('2020-03-02')->subWeek(1)->endOfWeek();
Considering the week starts at Monday and ends at Sunday.
You can do this way.
First get the current timestamp and subtract the no.of days you want.
$curTime = time();
echo date("Y-m-d",$curTime);
echo "<br />";
echo date("Y-m-d",($curTime-(60*60*24*7)));
$lastWeekStartTime = strtotime("last sunday",strtotime("-1 week"));
$lastWeekEndTime = strtotime("this sunday",strtotime("-1 week"));
$lastWeekStart = date("Y-m-d",$lastWeekStartTime);
$lastWeekEnd = date("Y-m-d",$lastWeekEndTime);
In order to find the last week start date and end date you can follow up this code for doing it so.
It works on all intervals to find the date interval.
$Current = Date('N');
$DaysToSunday = 7 - $Current;
$DaysFromMonday = $Current - 1;
$Sunday = Date('d/m/y', strtotime("+ {$DaysToSunday} Days"));
$Monday = Date('d/m/y', strtotime("- {$DaysFromMonday} Days"));
If so you need to change it with the datatime() you can perform this function.
$date = new DateTime();
$weekday = $date->format('w');
$diff = 7 + ($weekday == 0 ? 6 : $weekday - 1); // Monday=0, Sunday=6
$date->modify("-$diff day");
echo $date->format('Y-m-d') . ' - ';
$date->modify('+6 day');
echo $date->format('Y-m-d');
Using Functions:
If you want to find the last week range with the help of the functions you can preform like this.
Function:
// returns last week's range
function last_week_range($date) {
$ts = strtotime("$date - 7 days");
$start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
return array(
date('Y-m-d', $start),
date('Y-m-d', strtotime('next saturday', $start))
);
}
Usage:
$today=date();
print_r(last_week_range($today));
All the above functions that has been given will return the last week range irrespective of the start day of the week..
I am trying to build a function in PHP that, depending on the date will give me
The current week (mon-sun) 20/8/12 to 26/8/12
Following week+1 (mon-sun) 27/8/12 to 02/9/12
Following week+2 (mon-sun) 03/9/12 to 09/9/12
Following week+3 (mon-sun)
Following week+4 (mon-sun)
Following week+5 (mon-sun)
I have tried using the following, but is there anything cleaner??
$week0_mon = date("Y-m-d", strtotime(date("Y").'W'.date('W')."1"));
$week0_sun = date("Y-m-d", strtotime(date("Y").'W'.date('W')."7"));
$week1_mon = date("Y-m-d", strtotime(date("Y-m-d", strtotime($week0_mon)) . " +1 week"));
$week1_sun = date("Y-m-d", strtotime(date("Y-m-d", strtotime($week0_sun)) . " +1 week"));
echo $week0_mon.' to '.$week0_sun.'<br />';
echo $week1_mon.' to '.$week1_sun.'<br />';
Maybe this will answer to your issue, it calculates the previous monday and start from here to add one week at a time. Just edit the for
$dOffsets = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$prevMonday = mktime(0,0,0, date("m"), date("d")-array_search(date("l"),$dOffsets), date("Y"));
$oneWeek = 3600*24*7;$toSunday = 3600*24*6;
for ($i=0;$i<= 5;$i++)
{
echo "Week +",$i," (mon-sun) ",
date("d/m/Y",$prevMonday + $oneWeek*$i)," to ",
date("d/m/Y",$prevMonday + $oneWeek*$i + $toSunday),"<br>";
}
This gives me
Week +0 (mon-sun) 20/08/2012 to 26/08/2012
Week +1 (mon-sun) 27/08/2012 to 02/09/2012
Week +2 (mon-sun) 03/09/2012 to 09/09/2012
Week +3 (mon-sun) 10/09/2012 to 16/09/2012
Week +4 (mon-sun) 17/09/2012 to 23/09/2012
Week +5 (mon-sun) 24/09/2012 to 30/09/2012
I've adjusted #Wr1t3r answer to give correct date ranges as follows:
function plus_week($addWeek=0){
$last_monday_timestamp=strtotime('-'.(date('N')-1).' days');
if($addWeek!=0){
if($addWeek>0) $addWeek='+'.$addWeek;
$last_monday_timestamp=strtotime($addWeek.' week', $last_monday_timestamp);
}
$end_week_timestamp = strtotime ('+6 days', $last_monday_timestamp);
return date('d/m/y', $last_monday_timestamp).' to '.date('d/m/y', $end_week_timestamp);
}
date('N') will give weekday number mon-sun (1-7) so if we subtract 1 from this we know how many days to go back to last monday. OR we could have used strtotime('last monday').
BUT this way ensures we dont go back to last monday if we are currently on monday.
Monday=1 so (1-1=0) -0 days = today
Friday=5 so (5-1=4) -4 days = monday
SUNDAY=7 (not 0 if we used 'w') so (7-1=6) -6 days = monday (not tomorrow)
I've also adjusted this to do negative week numbers too.
I did it like this. I'm not sure if it is exactly what you want.
function plus_week($addWeek){
$date = date("d.m.Y",time());
$newdate = strtotime ( '+'.$addWeek.' week' , strtotime ( $date ) ) ;
$newdate = date ( 'd/m/y' , $newdate );
return $newdate;
}
for($i = 1; $i < 7; $i++){
echo "Following week+".$i." ".plus_week($i)." to ".plus_week($i+1)."<br/>";
}
From this you will get answer like this:
Following week+1 29/08/12 to 05/09/12
Following week+2 05/09/12 to 12/09/12
Following week+3 12/09/12 to 19/09/12
Following week+4 19/09/12 to 26/09/12
Following week+5 26/09/12 to 03/10/12
Following week+6 03/10/12 to 10/10/12
Does PHP calculate weeks as being Sunday - Saturday? For a given date I am trying to determine the beginning and ending date of it's week as well as the beginning date of the next/previous weeks. Everything works fine unless I pass in a Sunday and it thinks the date is in a previous week.
$start = $_GET['start'];
$year = date('Y', strtotime($start));
$week = date('W', strtotime($start));
$sunday = strtotime($year.'W'.$week.'0');
$next = strtotime('+7 Days', $sunday);
$prev = strtotime('-7 Days', $sunday);
echo '<p>week: ' . $week . '</p>';
echo '<p>sunday: ' . date('Y-m-d', $sunday) . '</p>';
echo '<p>next:' . date('Y-m-d', $next) . '</p>';
echo '<p>prev: ' . date('Y-m-d', $prev) . '</p>';
Outcome:
2011-01-09 (Sunday)
Week: 01
WRONG
2011-01-10 (Monday)
Week: 02
RIGHT
2011-01-15 (Saturday)
Week: 02
RIGHT
PHP doesn't think about weeks at all, if you're getting the wrong results, it's because your math is off. :)
$date = strtotime('2011-1-14');
$startingSunday = strtotime('-' . date('w', $date) . ' days', $date);
$previousSaturday = strtotime('-1 day', $startingSunday);
$nextWeekSunday = strtotime('+7 days', $startingSunday);
As Dr.Molle point out, the information about "W" is correct. Your problem is here:
$sunday = strtotime($year.'W'.$week.'0');
$sunday = strtotime($year.'W'.$week.'0');
$next = strtotime('+7 Days', $sunday);
$prev = strtotime('-7 Days', $sunday);
Then you called strtotime on a Timestamp object (sorry, I don't know the exact term).
The wrong type of parameter (timestamp and string are used not correctly) is the cause of the problem. Here's my piece of code to determine the week and the beginning day of the week:
<?php
$date = '2011/09/09';
while (date('w', strtotime($date)) != 1) {
$tmp = strtotime('-1 day', strtotime($date));
$date = date('Y-m-d', $tmp);
}
$week = date('W', strtotime($date));
echo '<p>week: ' . $week . '</p>';
?>
To fully understand, you should take a look on date & strtotime manual.
As defined in ISO_8601, what date('W') refers to, a week starts with monday.
But be careful and read about the ISO-week: http://en.wikipedia.org/wiki/ISO_week_date
Maybe the result is not always like expected.
example:
date('W',mktime(0, 0, 0, 1, 1, 2011))
It will return 52 instead of 01, because the first ISO-week of a year is the first week with at least 4 days in the given year.
As 2011-1-1 was a saturday, there are only 2 days, so 2011-1-1 is in ISO in the last week of 2010(52) and not in the first week of 2011.
The function date('W') uses the ISO-8601 definition, therefore Monday is the first day of the week.
In place of date('W') use strftime('%U').
Example:
$date = strtotime('2011-01-09');
echo strftime('%U',$date);
Outcome:
02
The code:
$date = strtotime('2012-05-06');
$sunday = date('Y-m-d', strtotime(strftime("%Y-W%U-0", $date)));
$sturday = date('Y-m-d', strtotime(strftime("%Y-W%U-6", $date)));
echo $sunday . "\n";
echo $saturday;
Outcome:
2012-05-06
2012-05-12
I am having trouble adjusting a week range in PHP.
I have it working to show the current work week starting on monday.
So for this week it shows 11/29/2010 - 12/03/2010.
I need to modify this to start from the current work week's monday and show and end date of two fridays from the monday.
So for example currently it would show a start date of 11/29/2010 and and end date of 12/10/2010.
Here is my code
<?
$timestamp = time();
echo date("m/d/Y", strtotime("this monday", $timestamp));
echo " - ";
echo date("m/d/Y", strtotime("Next Friday", $timestamp));
?>
How do I add +7 to "Next Friday"
thx
You cannot use this monday to construct date time,
as it went pass Monday, it return next Monday
here is my suggestion
<?
$current_wkday = date('N', time());
switch ($current_wkday)
{
/* assuming on sunday, get next monday */
case 0: $this_monday = strtotime('+1 day'); break;
case 1: $this_monday = time(); break;
default: $this_monday = strtotime('-'.($current_wkday-1).'day'); break;
}
echo date("m/d/Y", $this_monday); /* 11days = 7+(5-1) */
echo " - ";
echo date("m/d/Y", $this_monday+(86400*11));
?>
Instead of strtotime("Next Friday", $timestamp) do strtotime("Next Friday", $timestamp) + 60*60*24*7 (add the number of seconds in a week).
Just realised that "previous monday" only works the current day is tuesday or later...but it's a starting point for you..
<?php
$monday = date('m/d/Y', strtotime("previous monday"));
$friday = date('m/d/Y', strtotime($monday . " + 11 days"));
echo $monday . ' - ' . $friday ;
?>