I am making a page to let the clients choose a date for an appointment, so I need to build a list of the dates like this :
always begin from tomorrow, end by 6 months
always from Mondy to Saturday, no Sunday
the day of the week need to be in chinese, like "Monday" is "星期一", but the timezone is in France
Here is the php
date_default_timezone_set('Europe/Paris');
$tomorrow = date("Y年m月d日 l", time() + 86400);
$end = date("Y年m月d日 l", time() + 86400 * 7); // just 7 days for a try
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($tomorrow, $interval, $end);
foreach ($daterange as $date) {
echo $date . '<br/>';
}
This code is not working.
I need to build an array, which store all the dates of next 6 months, begin from tomorrow, without Sunday, the days need to be in chinese and the timezone needs to be in Europe, is that possible?
I think strtotime and array_push is what you are looking for. Try this:
$curDate = date('Y-m-d', strtotime('+1 day'));
$endDate = date('Y-m-d', strtotime('+6 months +1 day'));
$myArr = array();
while ($endDate >= $curDate) {
if (date('w', strtotime($curDate)) !== '0') array_push($myArr, $curDate);
$curDate = date('Y-m-d', strtotime($curDate . " +1 days"));
}
var_dump($myArr);
For the Options, create an array from sunday to saturday.
$weekdays = array('Sunday', ..., 'Saturday');
echo date('Y/m/d', strtotime($curDate)) . ' ' . $weekdays[date('w', strtotime($curDate))];
How can I get the current day, tomorrow and next day in PHP but ignore weekends?
I have already tried this code, but it will include Saturday and Sunday.
array(
'todayDate' => date('d/m/Y')
'tomorrowDate' => date('d/m/Y', strtotime(' +1 day')),
'nextDay' => date('l', strtotime(' +2 day'))
)
Thanks.
Use Weekday(s) with strtotime
date('l', strtotime(' +2 Weekdays'));
Fiddle
This finds the next weekday from a specific date (not including Saturday or Sunday):
echo date('Y-m-d', strtotime('2011-04-05 +2 Weekday'));
You could also do it with a date variable of course:
$myDate = '2011-04-05';
echo date('Y-m-d', strtotime($myDate . ' +2 Weekday'));
Try this, Get date between two date without weekdays(Saturday and Sunday).
$startdate = '10-06-2015';
$endDate = '17-06-2015' ;
$Workingdays = getdateBettwoDate($startdate,$endDate);
print_r($Workingdays);
function getdateBettwoDate($startdate,$enddate)
{
$start = new DateTime($startdate);
$end = new DateTime($enddate);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
$x=0;
$a=array();
foreach ($period as $dt)
{
$temp=$dt->format("l d-m-Y");
$ArTemp=explode(' ',$temp);
if($ArTemp[0]=='Saturday' || $ArTemp[0]=='Sunday'){
}else{
$a[$x]=$ArTemp[1];
$x++ ;
}
}
$a[$x]=date('l', strtotime( $enddate))." ".$enddate;
return $a ;
}
I want to get 4 different dates with PHP: 1 day, 1 week, 1 month and 3 months.
Every date is from the current day, so lets say today is 2014-11-26.
get past dates based on current date.
$today = date("Y-m-d");
$one_day = $today - //how do I get yesterday from current day?
$one_week = $today - //how do I get one week from current day?
$one month = $today - //how do I get one month from current day?
$three_month = $today - //how do I get three month from current day?
The strtotime()-function is perfect for that approach.
Use something like
echo date( 'Y-m-d', strtotime("-1 day"));
echo date( 'Y-m-d', strtotime("-1 week"));
echo date( 'Y-m-d', strtotime("-1 month"));
echo date( 'Y-m-d', strtotime("-1 year"));
You can use the DateTime class:
$date = new DateTime('now');
echo $date->format('Y-m-d');
$date = new DateTime('-1 day');
echo $date->format('Y-m-d');
$date = new DateTime('-1 week');
echo $date->format('Y-m-d');
$date = new DateTime('-1 month');
echo $date->format('Y-m-d');
$date = new DateTime('-3 months');
echo $date->format('Y-m-d');
See Supported Date and Time Formats for more details.
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');