php get today tomorrow and next day but ignore weekends - php

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 ;
}

Related

Carbon custom start and end of the week by timestamp

I have a list of records with $clocked_in timestamps. I want to display its week start and end date, so I came up with this:
$timesheetWeekStartDate = Carbon::parse($clocked_in)->startOfWeek()->setTimezone($timezone)->addHours(12)->addMinute()->format('Y-m-d H:i');
$timesheetWeekEndDate = Carbon::parse($clocked_in)->endOfWeek()->setTimezone($timezone)->addHours(12)->addMinute()->format('Y-m-d H:i');
$timesheet->period = $timesheetsWeekStartDate . ' - ' . $timesheetWeekEndDate;
It seems to be working, but I would like to have customized start and end of week - add 12 hours ahead. So next week would start/end on 2022-05-23 12:01 - 2022-05-29 12:00, not 2022-05-23 23:59 - 2022-05-29 00:00.
For example, if $clocked_in is starts on Monday 05:00 I want it to display the previous week, not current(because its not 12:01 yet).
How I can achieve that by utilising Carbon?
Didn't find a solution in Carbon, so made it in plain DateTime, here is solution:
$timesheet->clocked_in = '2022-05-21 21:20:04';
$start = date('Y-m-d', strtotime('-2 week', strtotime($timesheet->clocked_in)));
$end = date('Y-m-d', strtotime('+2 week', strtotime($timesheet->clocked_in)));
$period = new DatePeriod(
new DateTime($start),
new DateInterval('P1W'),
new DateTime($end)
);
$periods = [];
foreach ($period as $periodDate) {
$start = $periodDate;
$end = clone $periodDate;
$periods[] = [
'start' => $start->setTimezone(new DateTimeZone($user->account->timezone))->add(new DateInterval('PT12H'))->add(new DateInterval('PT1M'))->format('Y-m-d H:i'),
'end' => $end->setTimezone(new DateTimeZone($user->account->timezone))->add(new DateInterval('P7D'))->add(new DateInterval('PT12H'))->format('Y-m-d H:i'),
];
}
foreach ($periods as $period) {
if (($timesheet->clocked_in >= $period['start']) && ($timesheet->clocked_in <= $period['end'])) {
$timesheets[$timesheetKey]->period = date('m/d/Y', strtotime($period['start'])) . ' - ' . date('m/d/Y', strtotime($period['end']));
}
}

PHP confusion over reversing a DatePeriod iterator

I'm trying to work out how to invert my date period without pushing the data into an array and reversing it.
Say I would like a list of the last 5 Saturdays the following code will produce:
$begin = new DateTime( date('Y-m-d', strtotime("last Saturday")));
$begin->modify("- 4 weeks");
$end = new DateTime( date('Y-m-d', strtotime("last Saturday")));
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1W');
$periods = new DatePeriod($begin, $interval ,$end);
foreach($periods as $date){
echo $date->format("Y-m-d") . "\n";
}
2021-07-31
2021-08-07
2021-08-14
2021-08-21
2021-08-28
I would like this list reversed. So I figured I could make use of the DatePeriod invert property. Both the start and end of the DatePeriod look correct, with a result showing in the current element. However, interating over $period returns nothing:
$begin = new DateTime( date('Y-m-d', strtotime("last Saturday")));
$begin = $begin->modify( '+1 day' );
$end = new DateTime( date('Y-m-d', strtotime("last Saturday")));
$end->modify("- 4 weeks");
$interval = new DateInterval('P1W');
$interval->invert = 1;
$periods = new DatePeriod($begin, $interval ,$end);
foreach($periods as $date){
echo $date->format("Y-m-d") . "\n";
}
Update
So I've found a solution that calculates a diff of days and passes that to the end param in the DatePeriod constructor. It's not ideal but is working. Note the diff->days had to be divided by the days interval to prevent additional dates pulling through.
$start = new \DateTime( date('Y-m-d', strtotime("last Saturday")));
$end = new DateTime( date('Y-m-d', strtotime("last Saturday")));
$end->modify("- 4 weeks");
$diff = $end->diff($start);
$interval = new \DateInterval('PT0S'); //0 duration
$interval->d = -7; //negative value
$period = new \DatePeriod($start, $interval, $diff->days / 7);
foreach ($period as $date) {
echo $date->format('Y-m-d') . PHP_EOL;
}

Return Monday to Sundays of Current Week

I was wondering how to grab from Monday (03-12-2018) to Sundays (03-18-2018) dates. I know that if I use 'this sunday' it will return Sundays dates but how would I do that for all the rest? Using this on every week day would result in Monday and Tuesday (From this standing point) to be 19 and 20.
date_default_timezone_set('America/New_York');
$the_date = date('Y-m-d', strotime("this sunday"));
Try this:
date_default_timezone_set('America/New_York');
$monday = date('Y-m-d', strtotime("monday this week"));
$sunday = date('Y-m-d', strtotime("sunday this week"));
$period = new DatePeriod(
new DateTime($monday),
new DateInterval('P1D'),
(new DateTime($sunday))->modify('+1 day')
);
foreach ($period as $date) {
echo $date->format('Y-m-d'), "\n";
}
Demo https://implode.io/9qLW1K

How to add date + 6 month but exclude Sundays using PHP

I am creating a scheduler script that if I executed a task today my next task will be 6 months from today (Semi-Annual) but my problem is people has no activity on Sundays so it must be adjusted on Monday instead.
How can I add 6 month to today's date but exclude Sundays, I am using PHP and able to add 6 months using below code so far.
My code is this.
<?php echo date('Y-m-d', strtotime('+6 months'));?>
Here's the answer: https://stackoverflow.com/a/12365635/6557808
Provides more than you're asking for and is pretty clear.
Here is a small function to it. Just pass from and end date
<?php
function number_of_working_days($from, $to) {
$workingDays = [1, 2, 3, 4, 5, 6]; # date format = N (1 = Monday, ...)
$from = new DateTime($from);
$to = new DateTime($to);
$to->modify('+1 day');
$interval = new DateInterval('P1D');
$periods = new DatePeriod($from, $interval, $to);
$days = 0;
foreach ($periods as $period) {
if (!in_array($period->format('N'), $workingDays)) continue;
$days++;
}
return $days;
}
echo number_of_working_days('2017-05-06', '2017-05-08');
?>
Complete Answer is here:--
function daysToAdd($startDate,$endDate)
{
$count=0;
While($startDate!=$endDate){
$lpcnt=0;
$begin = new DateTime($startDate);
$end = new DateTime($endDate);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ){
if($dt->format( "l" )=='Sunday'){
$count++;
$lpcnt++;
}
}
$day = date('D', strtotime($endDate));
if($day=='Sun'){
$count=$count+1;
$lpcnt=$lpcnt+1;
}
$startDate=date('Y-m-d', strtotime($endDate . ' +1 day'));
$endDate=date('Y-m-d', strtotime($endDate . ' +'.$lpcnt.' day'));
if($startDate!=$endDate){
daysToAdd($startDate,$endDate);
}else{
if(date('D', strtotime($startDate))=='Sun') {
$count=$count+1;
break;
}
}
}
return $count;
}
$currentDate=date('Y-m-d');
$postSixMonth=date('Y-m-d', strtotime("+5 months"));
$daysToadd=daysToAdd($currentDate,$postSixMonth);
$requiredDate=date('Y-m-d', strtotime($postSixMonth . ' +'.$daysToadd.' days'));
echo $requiredDate;exit;
As stated in the PHP.net date() documentation you can check for a specific day number.
So, using that knowledge you can either:
if (date("w", strtotime("+6 months")) == 0) {
echo date("Y-m-d", strtotime("+6 months +1 days"));
}
else {
echo date("Y-m-d", "+6 months");
}
or something in the lines of Bilal Ahmed's answer using a switch
switch (date("w", strtotime("+6 months")) == 0) {
case 0: // Sunday
echo date("Y-m-d", strtotime("+6 months +1 days"));
break;
case 1: // Monday
case 2: // Tuesday
case 3: // Wednesday
case 4: // Thursday
case 5: // Friday
case 6: // Saturday
echo date("Y-m-d", "+6 months");
break;
}
which might be better if there is something to do for every different day as date() will not be executed every time you perform a case. (Note: I did not compare speeds)
EDIT: oneliner based on the comment of blokish on the askers post
echo date("w", strtotime("+6 months")) == 0 ? date("Y-m-d", "+6 months +1 days") : date("Y-m-d", "+6 months");

php how to store monday to friday of the next 6 months begin from tomorrow in an array

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))];

Categories