I have one leave array,(retrive from table)
$leavearray=array("2019-10-22","2019-10-23","2019-10-25","2019-10-26","2019-10-28","2019-10-30");
If I give 2,it should return next 2nd working day-2019-10-27
If I give 1,it should return next 1st working day-2019-10-24
Can you help me to get this.
$priority=2;
$date="2019-10-22";
echo $this->checknextdate($date,$priority);
function checknextdate($date,$priority){
$leavearray=array("2019-10-22","2019-10-23","2019-10-25","2019-10-26","2019-10-28","2019-10-30");
do{
if(in_array($date,$leavearray))
{
$date = date('Y-m-d', strtotime($date.'+1 day'));
}else{
return $date;
$checkok=1;
exit();
}
} while ($checkok==1);
}
I think you can do it like this, using your original code, although this might not be the perfect solution:
$priority = 2;
$date = "2019-10-22";
echo checknextdate($date,$priority);
function checknextdate($date, $priority){
$leavearray = array("2019-10-22","2019-10-23","2019-10-25","2019-10-26","2019-10-28","2019-10-30");
do{
$date = date('Y-m-d', strtotime($date . '+1 day'));
if (!in_array($date, $leavearray)) {
$priority--;
}
} while ($priority > 0);
return $date;
}
Related
I am using Carbon to add number of days, it there a way to avoid using for and/or while loop?
Add the numbers of days ($skipDayBy) and add the number of days if found in $excludeDatesPublic or $excludeDatesManual?
For example working demo:
function calculateDate($skipDayBy = 0) {
$excludeDatesPublic = ['2019-08-28'];
$excludeDatesManual = ['2019-09-01'];
$date = Carbon::now();
for($i = 0; $i < $skipDayBy; $i++) {
$date = $date->addDays(1);
while(in_array($date->toDateString(), $excludeDatesPublic) || in_array($date->toDateString(), $excludeDatesManual))
{
$date = $date->addDays(1);
}
}
return $date->toDateString();
}
echo calculateDate(4);
Returned 2019-09-02 as expected if today date is 2019-08-27.
Maybe you're looking for https://github.com/kylekatarnls/business-day that allows you to add days skipping holidays.
Alternatively, you can use the periods:
$skipDayBy = 5;
$excludeDatesPublic = ['2019-09-01'];
$excludeDatesManual = ['2019-09-04'];
$exclude = array_merge($excludeDatesPublic, $excludeDatesManual);
$date = CarbonPeriod::create(Carbon::now(), $skipDayBy)
->addFilter(function (Carbon $date) use ($exclude) {
return !in_array($date->format('Y-m-d'), $exclude);
})
->calculateEnd();
var_dump($date); // 2019-09-06 18:50:17 if run at 2019-08-31 18:50:17
With PHP shortly
$day='2019-12-12';
$date = date('Y-m-d', strtotime($day . " +4 days"));
echo $date;
Output
2019-12-16
Or u can use
$date= date('Y-m-d', strtotime('+4 days', strtotime($day)));
Using Carbon with laravel 5.6.
I want write a code that give me next occurrence of date from current date.
E.g Give next 31st May date
Scenario 1 :
Input : $currentDate = '01-30-2019'; // MM-DD-YYYY format
Expected Output: $next31May = '05-31-2019';
Scenario 2 :
Input : $currentDate = '07-04-2019'; // MM-DD-YYYY format
Expected Output: $next31May = '05-31-2020';
Update:
I tried below code but not satisfy
<?php
public function nextOccurance()
{
$now = Carbon::now();
$month= $now->month;
$year = $now->year;
if($month > 6)
{
echo Carbon::createMidnightDate($year+1, 5, 31);
}
else
{
echo Carbon::createMidnightDate(null, 5, 31);
}
exit();
}
?>
Thank You in advance.
public function nextOccurance()
{
// the 31th of May of the current year
$day = Carbon::createFromFormat('m-d', '05-31');
$now = Carbon::now();
// If today after $day
if($now >= $day) {
// Gat a next year
$day->modify('next year');
}
echo $day->format('Y-m-d');
exit();
}
this is like to get the next birthday.
class Test
{
public static function getNextBirthday($date)
{
// set birthday from current year
$date = Carbon::createFromFormat('m-d-Y', $date);
$date->year(Carbon::now()->year);
// diff from 31 may to now
// its negative than add one year, otherwise use the current
if (Carbon::now()->diffInDays($date, false) >= 0) {
return $date->format('m-d-Y');
}
return $date->addYear()->format('m-d-Y');
}
}
echo Test::getNextBirtday('05-31-1990');
I wish this will help you to fix informed issue.
$event = Carbon::parse('31 May');
if (Carbon::now() >= $event){
$nextEvent = $event->addYear();
} else {
$nextEvent = $event;
}
echo $nextEvent->format('m-d-Y');
Carbon provides a nice and a fluent interface to this kind of stuff.
You can lastOfMonth() to get the last day of month. for adding year you can add addYear(1)
$now = Carbon::now();
$month= $now->month;
$year = $now->year;
if($month > 6)
{
echo $now->addMonth(5)->lastOfMonth();
}
else
{
echo $now->addYear(1);
}
exit();
}
I have this code that modify date in the way I want, for example, if starting date is 31/01/2000, adding 1 month will return 29/02/2000, then, 31/03/2000.
If date is 30/01/2000 (not last day of the month) it will return 29/02/2000, then 30/03/2000, 30/04/2000 and so on.
I want to transfirm that code in a general case function, to be able to add 1,3,6,12 months, and inside the for loop, to work all corect.
I would like it to be a function 2 or 3 arguments, startingDate, duration(nr of iterations), frequency (add 1/3/6/12 months per once).
<?php
$date = new DateTime('2000-01-28'); // or whatever
#echo $date->format('d')." ".$date->format('t');
$expectedDay = $date->format('d');
$month = $date->format('m');
$year = $date->format('Y');
for ($i = 0; $i < 100; $i++) {
echo $date->format('Y-m-d') . "<br>";
if ($month++ == 12) {
$year++;
$month = 1;
}
$date->modify("${year}-${month}-1");
if ($expectedDay > $date->format('t')) {
$day = $date->format('t');
} else {
$day = $expectedDay;
}
$date->modify("${year}-${month}-${day}");
}
Whelp, there's an extremely easy function for this in PHP nowadays.
first, you get a timestamp instead of a datetime:
$timestamp = $date->getTimestamp();
Now, just use strtotime to add onto the date.
strtotime("+1 month", $myTimestamp);
You can change the +1 into anything you want, so you just throw the amount in the string and voila; a dynamic way of adding them!
however, since you want to do +30 days instead of a natural month, you're better off just adding 30 days to the timestamp, like so:
$timestamp = $timestamp + (3600 * 24 * 30); //s per h * h per d * d
So, you'd end up with something like this:
function calculateTime($startingDate, $iterations, $frequency){
$timeStamp = strtotime($startingDate);//if you expect a string date
$timeToAdd = (3600 * 24 * 30) * $frequency; //30 days * frequency
$return = array();
$return[] = date('Y-m-d', $timeStamp); //Original date
$previousDate = $timeStamp; //Original date for now
for($i = 0; $i < $iterations; $i++){
$newDate = $previousDate + (3600 * 24 * 30);
$return[] = date('Y-m-d', $newDate);
$previousDate = $newDate;
}
return $return;
}
And then for the rendering part:
//Let's render this stuff
$dates = calculateTime('24-08-2017', 25, 3);
foreach($dates as $date){
echo "$date</br>";
}
If you'd like to do it with full months, something like this:
<?php
function calculateTime($startingDate, $iterations, $frequency){
$timeStamp = strtotime($startingDate);//if you expect a string date
$return = array();
$return[] = date('Y-m-d', $timeStamp); //Original date
$previousDate = $timeStamp; //Original date for now
for($i = 0; $i < $iterations; $i++){
$lastDay = false;
//It's the last day of the month
if(date('t', $timeStamp) == date('d', $timeStamp)){
$lastDay = true;
}
if($frequency == 12){
$newDate = strtotime('+1 year', $previousDate);
}
else{
if($lastDay){
$firstDayOfMonth = strtotime(date("01-m-Y", $previousDate));
$newDate =strtotime("+$frequency month", $firstDayOfMonth);
}
else{
$newDate = strtotime("+$frequency month", $previousDate);
}
}
if($lastDay){
$return[] = date('Y-m-t', $newDate);
}
else{
$return[] = date('Y-m-d', $newDate);
}
$previousDate = $newDate;
}
return $return;
}
//Let's render this stuff
$dates = calculateTime('31-01-2000', 25, 1);
foreach($dates as $date){
echo "$date</br>";
}
I hope this helps? :)
If you'd like to see how this works quickly, just paste my code into a phpfiddle. Unfortunately the save function is broken right now.
I'm getting dates from a Wordpress field and I need to check if the dates have past or still to come.
$dates = ['date'=>'02/12/13','date'=>'10/12/14','date'=>'14/01/15'];
foreach ($dates as $date){
$the_date = $date['date'];
echo $the_date;
echo " ";
echo date('d/m/y');
echo " ";
if($the_date < date('d/m/y')){
echo 'gone';
}else{
echo 'to come';
}
}
The foreach echos out this.
02/12/13 22/11/14 gone
10/12/14 22/11/14 gone
14/01/15 22/11/14 gone
27/01/15 22/11/14 to come
10/02/15 22/11/14 gone
It looks like it's just checking the first day date.
A better option is to use the DateTime class. It allows to compare two DateTime instances using comparison operators.
$dates = ['02/12/13','10/12/14','14/01/15'];
foreach ($dates as $date) {
$the_date = \DateTime::createFromFormat('d/m/y', $date);
$now = new \DateTime();
echo $date." ".($the_date < $now ? 'gone' : 'to come')."\n";
}
The problem you see is because the dates are being compared as strings. The current date is "22/11/14" so it will be greater than any other date with a day starting with "1" or "0".
PD: Your array contains many elements using the same 'date' key. That is a problem so I've removed them in my example.
<?php
$dates = array('02/12/13','10/12/14','14/01/15');
$now = mktime(0,0,0);
foreach($dates as $date) {
$tmp = explode('/',$date);
$date_time = mktime(0,0,0,intval($tmp[1]),intval($tmp[0]),intval($tmp[2]));
echo $date . ' ' . ($now > $date_time?'gone':'to come') . "\n";
}
Use PHP's DateTime API :
$date='02/12/13';
if(\DateTime::createFromFormat('d/m/y',$date) < new \DateTime()){
//date is in the past
}else{
//date is either today or in the future
}
Offical PHP doc:
http://php.net/manual/en/class.datetime.php
Best way is to use timestamp: Try this:
foreach ($dates as $date){
$the_date = $date['date'];
echo $the_date;
echo " ";
echo date('d/m/y');
echo " ";
if( strtotime($the_date) < time() )
{
echo ' is gone';
}
else
{
echo ' is to come';
}
}
Keep her simple:
// $date is the date you need to compare to today
$date = ("2015 10 03");
// Make sure their formats are purely numeric and match
if ($date->format('m.d.y') >= date('m.d.y'))
{
your procedure...
}
I suggest using the capabilities of the DateTime class instead. Then you can do the check as follows:
<?php
$then = $reset_date;
$then = new DateTime($then);
$now = new DateTime(date("m-d-Y"));
$sinceThen = $then->diff($now);
$new = new DateTime($reset_date);
$old = new DateTime(date("m-d-Y"));
if ( $old->modify('+1 year') < $new) {
echo "<font color='red'>Reset now <br></font>";
echo "<font color='orange'>$sinceThen->y years <br></font>";
echo "<font color='orange'>$sinceThen->m months </font>";
echo "<font color='orange'>$sinceThen->d days have passed.<br></font>";
} else {
echo "<font color='green'> $sinceThen->y years <br>
$sinceThen->m months $sinceThen->d days till to Reset.</font>";
//Combined
}
?>
I need to Write a function named countDays which takes a single parameter named dateinstring which is string in the form ”MM.DD.YYY” represent a real date value. The function should print to the console the number of days from the beginning of the year specified in dateInString until the date represented in dateInString. If the value of dateInString is invalid, the function should print ”Bad format” to the console.
I have written the code as below :
function countDays($dateInString){
date_default_timezone_set('America/Los_Angeles');
$date = explode('.', $dateInString);
if(count($date) == 3 && checkdate($date[0], $date[1], $date[2])){
$formatted_date = $date[2].'-'.$date[0].'-'.$date[1].'00:00:00';
$diff = strtotime($formatted_date).'-'.strtotime($date[2].'-01-01 00:00:00');
echo round($diff/86400)+1;
}
else {
echo 'Bad format';
}
};
countDays('1.15.2014');
But the above code seems that not giving the correct output. It is about 33% correct. But where is the problem with this code ? Please help me!!!
$diff = strtotime($formatted_date).'-'.strtotime($date[2].'-01-01 00:00:00');
Change to
$diff = strtotime($formatted_date) - strtotime($date[2].'-01-01 00:00:00');
You made the minus symbol a string instead of an operator.
You could try it this way
function countDays($dateInString) {
date_default_timezone_set('America/Los_Angeles');
$date = explode('.', $dateInString);
if (checkdate($date[0], $date[1], $date[2])) {
$year_start = mktime(0, 0, 0, 1, 1, $date[2]);
$your_date = mktime(0,0,0,$date[0], $date[1], $date[2]);
$diff = $your_date - $year_start;
echo floor($diff /(60*60*24));
} else {
echo "Bad date supplied";
}
}
A better approach would be to use the DateTime class. I haven't included the validation in this, but i suggest you use regex for that.
function countDays($dateInString){
$parts = explode('.', $dateInString);
$date = new DateTime($parts[2] . '-' . $parts[0] . '-' . $parts[1]);
$compare = new DateTime( $date->format('Y') . '-01-01' );
$interval = $date->diff($compare);
return $interval->format('%a');
}
echo countDays('09.15.2014');
Check this out.
function countDays($dateInString){
date_default_timezone_set('America/Los_Angeles');
$date = explode('.', $dateInString);
if(count($date) == 3 && checkdate($date[0], $date[1], $date[2])){
$formatted_date = strtotime($date[2].'/'.$date[0].'/'.$date[1]);
$endTimeStamp = strtotime("2014/01/01");
$timeDiff = abs($endTimeStamp - $formatted_date);
echo round(intval($timeDiff/86400));
}
else {
echo 'Bad format';
}
};
countDays('01.01.2014');