How to get the next month on same day in php.
By using this code. I am able to get next month
$delidate = '2014-01-13'; // Today is Monday
$monthlyDate = strtotime("+1 month".$delidate);
$monthly = date("Y-m-d", $monthlyDate);
echo $monthly;
Output of this code is:- 2014-02-13
Now, I want to get the next month date on Monday only.
I need next month monday day 2014-02-10
or Suppose if day is Wednesday then i need Wednesday date on next month.
Updated:- Suppose if Particular day is not available on next date in that case i need nearest day in next month.
Update:-
I try this code
$delidate = '2014-01-29';
$monthlyDate = strtotime( $delidate . " +1 month " );
$monday = strtotime( date("Y-m-d", $monthlyDate) . " Monday this week " );
$monthly = date("Y-m-d", $monday);
echo $monthly;
It give me output 2014-03-03
while i need 2014-02-26
Try this
$delidate = '2014-01-13';
$monthlyDate = strtotime( $delidate . " +1 month " );
$monday = strtotime( date("Y-m-d", $monthlyDate) . " Monday this week " );
$monthly = date("Y-m-d", $monday);
echo $monthly;
You can do like this, since you want get same date after 1 Month
$delidate = '2014-01-13'; // Today is Monday
$monthlyDate = strtotime("+4 week".$delidate);
$monthly = date("Y-m-d", $monthlyDate);
echo $monthly;
Try this. This will take into consideration the current day not only Monday.
$delidate = '2014-01-13';
$monthlyDate = strtotime( $delidate . " +1 month " );
$dayname = strtotime( date("Y-m-d", $monthlyDate) . " " . date('l', strtotime($delidate)) . " this week " );
$monthly = date("Y-m-d", $dayname);
echo $monthly;
Related
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 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>";
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 want to get the start and end date of the previous calender month.
So, it's July 2012 now, I want the function to return 01 June 2012 as start and 30 June 2012 as the end.
This is my code:
$current_month = date("Y-m-01 00:00:00");
$start_month = strtotime(date("Y-m-d", strtotime($current_month) . " -1 month"));
$end_month = strtotime(date("Y-m-d", strtotime($current_month) . " -1 second"));
echo "Start Month: " . date('Y-m-d',$start_month) . "( " . $start_month . ")<br>";
echo "End Month: " . date('Y-m-d',$end_month) . "( " . $end_month . ")<br>";
But it echo's:
Start Month: 2012-07-01( 1341093600)
End Month: 2012-07-01( 1341093600)
Any idea what I'm doing wrong?
Answering whats wrong with the code you posted, you just needed to move your round bracket over a little bit and you had it. :)
<?php
$current_month = date("Y-m-01 00:00:00");
$start_month = strtotime(date("Y-m-d", strtotime($current_month . " -1 month")));
$end_month = strtotime(date("Y-m-d", strtotime($current_month . " -1 day")));
echo "Start Month: " . date('Y-m-d',$start_month) . "( " . $start_month . ")<br>";
echo "End Month: " . date('Y-m-d',$end_month) . "( " . $end_month . ")<br>";
?>
Try this, (check the manual of strtotime)
$now = time();
$current_month = date("Y-m-01 00:00:00", $now);
$start_month = strtotime("-1 month", $now);
$end_month = strtotime("-1 second", $now);
Try something like this:
echo date("Y-m-d", mktime(0,0,0,date('m')-1,1,date('y')));
echo date("Y-m-d", mktime(0,0,0,date('m'),0,date('y')));
Seems a little bit oversized, what you (and the other answers :X) tried. It's justs
echo date('Y-m-d', strtotime('first day of last month'));
echo date('Y-m-d', strtotime('last day of last month'));
For arbitrary months
// 3 months in the past
echo date('Y-m-d', strtotime('first day of -3 months'));
echo date('Y-m-d', strtotime('last day of -3 months'));
// 3 months in the future
echo date('Y-m-d', strtotime('first day of +3 months'));
echo date('Y-m-d', strtotime('last day of +3 months'));
Read more At the manual
echo $firstdate= "01/".date('m')."/".date('Y') ;
$lastdateofmonth=date('t',date('m'));
echo $lastdate=$lastdateofmonth."/".date('m')."/".date('Y') ;
or
$date='2012-06-05 12:00:00';
echo "End = ".date("Y-m-t",strtotime($date));
echo "start = ".date("Y-m-01",strtotime($date));
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