Calculating next business day - php

How can I calculate Next Business Day given a Zend_Date and a cutoff time of 5pm? Business days are M-F (weekdays).
Example:
Fri 4 pm should return same date
Sat anytime should return next Mon
Tue 8 pm should return Wed

I think this has been asked before, Next business day of given date in PHP, but here it is using Zend_Date:
$now = new Zend_Date();
if (($now->get(Zend_Date::WEEKDAY_DIGIT) % 6 == 0)
|| ($now->isLater('17:00:00', Zend_Date::TIMES))
) {
$now->set(
strtotime('+1 weekday', $now->toString(Zend_Date::TIMESTAMP)),
Zend_Date::TIMESTAMP
);
}
echo $now->toString(Zend_Date::W3C);

Related

Php Date Function with complexity

My question: if the date is on Monday then my timer should start from 8:30 am and should calculate the time difference from 8:30 till date assigned, in this case its 2017-06-02 14:20:00 for first one. So time difference should be 5 hours 50 min.
Second case, date created on 2017-06-02 09:50:00 and date assigned is: 2017-06-03 13:20:00. SO it should calculate from 9:50 till 9:00pm and again start from 8:30 till 13:20:00 (if next day lies on mon-sat. If next day is sun then timer should calculate from 11am till 1:20pm. and should give me duration in hours and minutes.
How would I do that? Its in Php & MySQL. I am not any frameworks or CMS systems. Its native php.
My Data:
Date Created: 2017-06-02 02:50:00
Date Assigned: 2017-06-02 14:20:00
Date Created: 2017-06-02 09:50:00
Date Assigned: 2017-06-03 13:20:00
Mon - Sat = 8:30am - 9:00pm
Sunday = 11am - 5pm
Thank you in advance.
Edited: This function checks first what day is your dateassigned, then calculates the time difference based on your criteria.
<?php
echo getTimeLapsedCustom('2017-06-02 14:20:00') . "<br>";
echo getTimeLapsedCustom('2017-06-03 13:20:00') . "<br>";
function getTimeLapsedCustom($time){
$timecreated = '';
$timeassigned = $time;
$datetime2 = DateTime::createFromFormat('Y-m-d H:i:s', $timeassigned);
if($datetime2->format('D') === 'Sun'){
$timecreated = $datetime2->format('Y-m-d') . ' 11:00:00';
}else{
$timecreated = $datetime2->format('Y-m-d') . ' 08:30:00';
}
$datetime1 = DateTime::createFromFormat('Y-m-d H:i:s', $timecreated);
$interval = $datetime1->diff($datetime2);
return $interval->format('%h hours %i min');
}
Results:
5 hours 50 min
4 hours 50 min

Daylight savings calculation in Php

In my application I have varchar field in mysql for 365 days in particular format i.e. '0000-04-12' (12th of April). Right now I am having a function that should check if particular date from field falls in daylight saving zone of Calgary-Canada, which ranges from 2:00 pm Second Sunday of March to 2:00 pm First Sunday of November. and returns true or false accordingly.
<?php
/*
#param $time form Database e.g. 0000-04-12
*/
function isDaylightSaving($time){
// to replace 0000 in start with current year
$today = substr_replace($time,date('Y'),0,4);
$date = new DateTime($today);
$week = $date->format('W');
// TO DO
// find date falls between 2:00 pm Second Sunday of March to 2:00 pm First Sunday of November.
return ;
}
?>
Right now its returning the week with respect to particular year but I need to check week with respect to month to perform particular logic, which I am unable to get.Thanks.
Try this:
if($time > date('Y-m-d',strtotime('second sunday of march')) &&
$time < date('Y-m-d',strtotime('first sunday of november'))){
return true;
}else{
return false;
}
strtotime() is a great function.
The example given by #Ezenhis sounds good, but lacks of some elements.
Extending it:
$low = new DateTime('#'.strtotime('second sunday of march'));
$high = new DateTime('#'.strtotime('first sunday of november'));
if($date > $low && $date < $high) {
// we're good
}
You shouldn't compare strings from date functions. Using DateTime is also a better way to handle dates/times in PHP since PHP 5.0.

php previous next week based on selected date

I'm trying to create a simple weekly agenda. Here is my code:
$daterange = new DatePeriod(
new DateTime('2016-04-08'),
new DateInterval('P1D'),
new DateTime(date('Y-m-d',strtotime ( '1 week' , strtotime ( '2016-04-08' ) )))
);
Then a simple loop:
foreach ($daterange as $k => $row) {
echo $row->format('d') ." - " . $row->format('D') . "<br>";
}
Produces something like:
08 - Fri
09 - Sat
10 - Sun
11 - Mon
12 - Tue
13 - Wed
14 - Thu
Which works perfectly fine, but what I need is to display dates starting from Sunday or Monday, based on first day of the week. The desired result should be:
10 - Sun
11 - Mon
12 - Tue
13 - Wed
14 - Thu
15 - Fri
16 - Sat
You could use something like this:
date('Y-m-d', strtotime('last Monday', '2016-04-08'));
You can replace last Monday with next Monday/last Sunday/next Sunday, depending on what you need. This will give you the previous/next first day of the week for current day. You could then obtain a 7 day interval, starting from this date.
It is generally considered bad to mix DateTime and strtotime(), date(), etc., so here's another solution:
// The question is ambiguous on this, so we'll use a
// variable to configure the preferred first weekday
$firstWeekDay = 'Sunday';
$dateTime = new DateTime();
// If today isn't the first week day, get the last one
if ($dateTime->format('l') !== $firstWeekDay)
{
$dateTime->modify("last {$firstWeekDay}");
}
$period = new DatePeriod(
$dateTime,
new DateInterval('P1D'),
6 // Just tell PHP to create 6 (7 - the 1 start day) more dates
);

current week contains the last friday of the month

I am trying creating a script that will change an image on a page if the last friday in the month is during the current week. For example if I am on any of the day of week (Monday to Sunday) that contains the last friday of the month during the week I will get an output that differs from the rest of the month.
I was helped on a previous question with this code but it only works if the last day of the month is today. However I need the function to know if the last day of the month is on either Monday, Tuesday, Wednesday, Thursday is still in the current week as my week runs from Monday to Sunday:
// Be sure to check your timezone `date_default_timezone_set`
$today = new DateTime();
$last_friday = new DateTime('last Friday of this month');
// For testing
$friday_april = new DateTime('2014-4-25');
if ($today->format('Y-m-d') === $last_friday->format('Y-m-d')) {
print 'Today is friday';
}
if ($friday_april->format('Y-m-d') === $last_friday->format('Y-m-d')) {
print 'Yes, a test friday is also a friday';
}
Any help would be great!
Change your date format for the comparisons.
W should suffice.
Why?!
Because then the same string (the ISO week number) will be produced for dates within the same week (beginning on Mondays).
Given this month, April 2014, the week number of the week containing the last Friday is 17.
2014-04-19 Sat => 16 ✗
2014-04-20 Sun => 16 ✗
2014-04-21 Mon => 17 ✓
2014-04-22 Tue => 17 ✓
2014-04-23 Wed => 17 ✓
2014-04-24 Thu => 17 ✓
2014-04-25 Fri => 17 ✓
2014-04-26 Sat => 17 ✓
2014-04-27 Sun => 17 ✓
2014-04-28 Mon => 18 ✗
2014-04-29 Tue => 18 ✗
2014-04-30 Wed => 18 ✗
Summary
if ($today->format('W') === $last_friday->format('W')) {
// Do victory dance
}
You need a loop. Go through the loop and add a day until you get to the next month. Count how many Fridays you encounter (including today) from today to the start of the next month. If its only 1, then the last Friday is in this week.
use strtotime and date so it should look like this:
$today = new DateTime();
$last_friday = strtotime('last Friday of this month');
// For testing
$friday_april = new DateTime('2014-4-25');
if ($today->format('Y-m-d') === date('Y-m-d', $last_friday)) {
print 'Today is friday';
}
if ($friday_april->format('Y-m-d') === date('Y-m-d', $last_friday)) {
print 'Yes, a test friday is also a friday';
}
$today = getdate();
$weekStartDate = $today['mday'] - $today['wday'];
$weekEndDate = $today['mday'] - $today['wday']+6;
echo "week start date:".$weekStartDate;
echo "<br/>";
echo "week end date:".$weekEndDate;
By this code you can get start and end days of the current week
$thisWeekHasLastFridayOfMonth = function () {
$lastFridayThisMonth = date('Y-m-d',strtotime('last Friday of this month'));
$testDate = date('Y-m-d',strtotime('today'));
$thisWeekSunday = (date('N',strtotime($testDate))!=1?date('Y-m-d',strtotime('last Sunday')):date('Y-m-d'));
$thisWeekSaturday = (date('N',strtotime($testDate))!=7?date('Y-m-d',strtotime('next Saturday')):date('Y-m-d'));
//echo $lastFridayThisMonth . '<br>' . $thisWeekSunday . '<br>' . $thisWeekSaturday;
if (strtotime($lastFridayThisMonth) >= strtotime($thisWeekSunday) &&
strtotime($lastFridayThisMonth) <= strtotime($thisWeekSaturday))
return true;
else
return false;
};
echo $thisWeekHasLastFridayOfMonth?'True':'False';

Find the week number in month with a calendar starting on a Friday

I'm looking to find which week a date is in for a month where the week begins on a Friday. So for example, the calendar would look like this:
Week count F Sa Su M Tu W Th
----------------------------------------
0 1 2 3 4 5 6
1 7 8 9 10 11 12 13
2 14 15 16 17 18 19 20
3 ...
The input will be the date in timestamp format ($currentDate) and I am looking for output in the form of $weekCount.
Thanks
Just need to count the number of previous fridays by going back 7 days at a time until you find you have covered them all.
The loop will always go beyond the first friday to cover the normal case of Friday not being the first day in the month. If Friday IS the first day in the month, then you need to reduce the increment to account for the overshoot.
$currentDate = time(); // e.g.
$dayOfMonth = date('j', $currentDate);
$ym = date('Y-m', $currentDate);
$firstFriday = strtotime("Friday ".$ym);
$dateOfFirstFriday = date('j', $firstFriday);
// we need to reduce count by 1 if friday is the 1st, to compensate
// for overshooting by a week due to the >= in the while loop
$weekCount = ($dateOfFirstFriday == 1) ? -1 : 0;
while ($dayOfMonth >= $dateOfFirstFriday) {
$weekCount ++;
$dayOfMonth = $dayOfMonth - 7;
}
var_dump($weekCount);
This code doesn't account for if we are on say Fri 30th, then actually are we in week 0 of the next month? Not clear though from opening post if that is required.

Categories