I'm trying to get a week range for the month, I've somewhat accomplished what I'm looking for but I'd like the code to be more reliable. The data ranges are going to be used to some MySQL queries where I can filter out data by data range.
What I'd like to achieve is to get something like the Windows 10 calendar. So the result is
2020-03-01 to 2020-03-01
2020-03-02 to 2020-03-08
2020-03-09 to 2020-03-15
2020-03-16 to 2020-03-22
2020-03-23 to 2020-03-29
2020-03-30 to 2020-03-31
CODE
$start_date = date("Y-m-d", strtotime("first day of this month"));
$end_date = date("Y-m-d", strtotime("last day of this month"));
function getWeekDates($date, $start_date, $end_date) {
$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$from = date("Y-m-d", strtotime("{$year}-W{$week}+1"));
if($from <= $start_date){
$from = $start_date;
}
$to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));
if($to >= $end_date){
$to = $end_date;
}
echo $from." to ".$to.'<br>';
}
for($date = $start_date; $date <= $end_date; $date = date('Y-m-d', strtotime($date. ' + 7 day'))){
echo getWeekDates($date, $start_date, $end_date);
echo "\n";
}
Which results in
It's missing the last two days of the month (30 and 31).
2020-03-01 to 2020-03-01
2020-03-02 to 2020-03-08
2020-03-09 to 2020-03-15
2020-03-16 to 2020-03-22
2020-03-23 to 2020-03-29
You can use DateTime objects to do this more readily, setting a start time as the first Monday on or before the start of the month, and then adding a week at a time until it is past the end of the month:
$month_start = new DateTime("first day of this month");
$month_end = new DateTime("last day of this month");
// find the Monday on/before the start of the month
$start_date = clone $month_start;
$start_date->modify((1 - $start_date->format('N')) . ' days');
while ($start_date <= $month_end) {
echo max($month_start, $start_date)->format('Y-m-d') . ' to ' . min($start_date->modify('+6 days'), $month_end)->format('Y-m-d') . "\n";
$start_date->modify('+1 day');
}
Output:
2020-03-01 to 2020-03-01
2020-03-02 to 2020-03-08
2020-03-09 to 2020-03-15
2020-03-16 to 2020-03-22
2020-03-23 to 2020-03-29
2020-03-30 to 2020-03-31
Demo on 3v4l.org
I want dates like if today is Tuesday then I want a date from starting of this week to the current day and get last week date from starting of last week to the current day like if today is 11-02-2020 and Tuesday then I want date like 10-02-2020 11-02-2020 and last week dates like 03-02-2020 04-02-2020
for that, I have used below functions
function last_week_date(){
date_default_timezone_set("Asia/Kolkata");
$day = date("l");
$previous_week = strtotime("-1 week +1 day");
$start_week = strtotime("last monday",$previous_week);
$end_week = strtotime("next $day",$start_week);
$start_week = date("Y-m-d",$start_week);
$end_week = date("Y-m-d",$end_week);
return $start_week.' '.$end_week ;
}
function this_week_date(){
date_default_timezone_set("Asia/Kolkata");
$day = date("l");
$previous_week = strtotime("+1 day");
$start_week = strtotime("last monday",$previous_week);
$end_week = strtotime("next $day",$start_week);
$start_week = date("Y-m-d",$start_week);
$end_week = date("Y-m-d",$end_week);
return $start_week.' '.$end_week ;
}
but with this, I have received dates of the full week not till a particular day
can anybody help me with this
This function will give you the results you want. You can simply pass it a parameter which is the number of weeks forward or backward to shift the dates to get this weeks/last weeks/next weeks days:
function week_date($weeks = 0) {
$start = new DateTime("monday this week");
$end = new DateTime();
$start->modify("$weeks weeks");
$end->modify("$weeks weeks");
return $start->format('Y-m-d') . ' ' . $end->format('Y-m-d');
}
To call, use
echo week_date() . "\n"; // this week
echo week_date(-1) . "\n"; // last week
echo week_date(1) . "\n"; // next week
Output (as of Monday 2020-02-10):
2020-02-10 2020-02-10
2020-02-03 2020-02-03
2020-02-17 2020-02-17
Demo on 3v4l.org
i have a script that generates week days with a next and preview. It workes perfect but i also added a callendar that is autosubmiting on change. the problem i have with it is that when i select a day and that day is monday is still goes me to last week. What i need is to verify if the selected day is monday and give $monday that date instead giving it the last week monday. Here is my script:
$date = strtotime(date($_GET['date']));
$monday = "";
if (isset($_GET['n_startdate'])) {
$date = $_GET['n_startdate'];
$lastweek = strtotime("next week", $date);
$monday = strtotime("last Monday", $lastweek);
} else if ( isset($_GET['p_startdate'])) {
$date = $_GET['p_startdate'];
$lastweek = strtotime("-7 days", $date);
$monday = strtotime("last Monday", $date);
} else if($date!='' ){
$monday = strtotime("last Monday", $date);
} else {
$date = strtotime(date('Y-m-d H:i:s'));
$monday = strtotime("last Monday", $date);
}
edit: i don't get why i got -1 to my question :(
If you want to find out if a given date is a day of the week, use the "D" flag for the date function.
if(date('D', $unixTimestamp) === 'Mon') {
//Do what you want here
}
You can find more details on the PHP manual page for the date function here - http://php.net/manual/en/function.date.php
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..
How to find get week duration or week range from monday to friday for the given date in php.
for example i have date
$date = "2013-02-24";
Now i want range of date from monday to friday for the month
Just Try Out this answer.
$date = "2013-02-24";
$week = date('W', strtotime($date));
$year = date('o', strtotime($date));
echo "first day of week". $from = date("Y-m-d", strtotime("{$year}-W{$week}-1"));
echo "end day of week". $to = date("Y-m-d", strtotime("{$year}-W{$week}-5"));
function week_range($date) {
$ts = strtotime($date);
$start = (date('w', $ts) == 0) ? $ts : strtotime('last monday', $ts);
return array(date('Y-m-d', $start),
date('Y-m-d', strtotime('next friday', $start)));
}
And call it like this:
list($start_date, $end_date) = week_range('2009-05-10');