I want to get number of weekends and number of bussiness day by I know only $startDate and $endDate, Is have any PHP's function that can calculate automatic ?
This is my code :
$endDate = strtotime($endDate);
$startDate = strtotime($startDate);
echo $days = ($endDate - $startDate) / 86400 + 1;
My code will return how many day, and how I need more is what day ?
How I can get it?
Example:
startDate:`2014-11-17`
endDate:`2014-11-19`
I want this Output:
it's 3 day
2014-11-17 is Monday
2014-11-18 is Tueday
2014-11-19 is Wendnesday
How about...
for ($time = $startDate; $time <= $endDate; $time += 86400) {
echo date('Y-m-d \i\s l', $time) . '<br>';
}
This might help you, or at least may give you some clue
$endDate = strtotime($endDate);
$startDate = strtotime($startDate);
echo $days = ($endDate - $startDate) / 86400 + 1;
$time = $startDate;
while ($time <= $endDate) {
echo date('Y-m-d', $time) . ' is ' . date('l', $time);
$time += 86400;
}
if ($time != $endDate) {
echo date('Y-m-d', $time) . ' is ' . date('l', $time);
}
This should work for you:
<?php
$startDate = strtotime("2014-11-17");
$endDate = strtotime("2014-11-19");
echo "It's " . $days = ($endDate - $startDate) / 86400 + 1 . " days";
for($count = 0; $count < $days; $count++)
echo "<br />" . date('Y-m-d', strtotime('+' . $count . ' day', $startDate)) . ' is ' . date('l', strtotime('+' . $count . ' day', $startDate));
?>
Output:
It's 3 days
2014-11-17 is Monday
2014-11-18 is Tuesday
2014-11-19 is Wednesday
There's no built-in. Excel has NETWORKDAYS(), but in PHP you must roll your own.
There is a PHP solution at Day difference without weekends
Note its limitations in the comments there e.g. if a public holiday falls on a weekend day.
The following page describes an alternative implementation of NETWORKDAYS(): http://www.cpearson.com/excel/betternetworkdays.aspx
It's not PHP but it demonstrates the logic.
Unfortunately there is no shortcut other than looping through each day in the period and deciding whether or not to count it. You need to accept arguments for (or hard-code) the weekend days and the dates of any public holidays (if you are excluding public holidays).
If you are doing this frequently and for long periods, you might pre-compute and cache the number of business days for each calendar month and year; then, at run-time, you look up the number of days in each whole year within the period, then the remaining whole months, then do the ordinary loop for the remaining days at the start and end of the period.
EDIT: If you just want to exclude weekends (not public holidays), then you can calculate 5 days for each whole week in the period, and then calculate any remaining days: https://github.com/bgarlock/scripts/blob/master/PHP%20Equivalent%20of%20MS%20Excel%20NETWORKDAYS%20function.php
Please find below code I am sure it works for you
NOTE : Parameter passed P1D is denoting 1 Day difference same way you can pass P1M for 1 month P1W for 1 week and P1Y for 1 Year.
$date1 = '29/08/2013';
$date2 = '03/09/2013';
function returnDates($fromdate, $todate) {
$fromdate = \DateTime::createFromFormat('d/m/Y', $fromdate);
$todate = \DateTime::createFromFormat('d/m/Y', $todate);
return new \DatePeriod(
$fromdate,
new \DateInterval('P1D'),
$todate->modify('+1 day')
);
}
$datePeriod = returnDates($date1, $date2);
foreach($datePeriod as $date) {
echo $date->format('d/m/Y'), PHP_EOL; //you can set any date format here
}
Related
I want to detect how to detect if the day is in the specific settings that I save.
I already save the settings
$checkday = "Sunday";
$period = "48"; // this in hours
So I need to check if today in this week is Sunday + 48 hours after (means Tuesday) then run a function.
I already make these code
$starttimer = date('Y-m-d 00:00:00', strtotime($checkday));
$endtimer = date('Y-m-d H:i:s', strtotime('+' . $period . ' hour', strtotime(date('Y-m-d 00:00:00'))));
if(time() >= strtotime($starttimer) && time() <= strtotime($endtimer)){
// this should run a function
}
This code is working if today is Sunday, but the problem is when today is Monday it will detect next Week Monday. I need it to check from this week Sunday + 48 hours after then run function.
The conclusion is I want to run the function on every week start from Sunday + 48 hours after.
Thanks for reading my problem, hope someone can help me.
I think you need get day of this week.
because if checkday = "Monday" then $starttimer will return Monday of next week.
<?php
$checkday = 'Monday';
$period = 48;
$day_this_week = "$checkday this week";
$starttimer = date('Y-m-d 00:00:00', strtotime($day_this_week));
$endtimer = date('Y-m-d H:i:s', strtotime('+' . $period . ' hour', strtotime(date('Y-m-d 00:00:00'))));
var_dump($starttimer);//string(19) "2022-09-12 00:00:00"
You can use the flexibility of strtotime to find the time of the last occurance of $checkDay. You also need to check to see that today is the check day and run in that condition as well.
<?php
$checkDay = 'Sunday';
$period = 48;
$isCheckDay = date('w', strtotime($checkDay)) === date('w');
$start = strtotime('last ' . $checkDay);
$end = strtotime(date('Y-m-d', $start) . ' +' . $period . ' hours');
$now = time();
if ($isCheckDay || ($now >= $start && $now <= $end)) {
echo 'run function';
}
It is not necessary to work with timestamps. Datetime objects can be directly compared. This makes the code easier to read.
$checkday = "Sunday";
$period = "2 Days";
$dtStart = date_create('Tomorrow')->modify('last '.$checkday);
$dtEnd = (clone $dtStart)->modify($period);
/* Use 'Now' instead of 'Today' in the following line
* if the time periods can also be fractions of days.
*/
$dtToDay = date_create('today');
if($dtToDay >= $dtStart AND $dtToDay < $dtEnd){
echo 'run function';
}
else {
echo 'do nothing';
}
$dtStart is always the last $checkday weekday before tomorrow. A time in hours can also be entered for $period, for example "48 hours".
Demo: https://3v4l.org/5d2Xt
You can use below code for fulfill your requirement...
<?php
$checkday = "Sunday";
$period = "48"; // this in hours
echo date('l', strtotime($checkday. ' + '.$period.' hours'));
?>
I want to create a list where a week is displayed and the values of each day are displayed in the section of the day.
First I tried to create the dynamic week change so that you can press a button to show the week before or the next week.
Now I have this code and in the output I have a week from Sunday to Saturday.
Now here is my problem, since I always have Sunday as the first day the week that is output is also always the one from Sunday, so the week before the actual week.
I tried to change the week output in the variable $dow with date('N') from date('n'). Unfortunately this did not work.
How do I have to change my code to show / output the correct week from Monday to Sunday?
Note: under $ts += 0 * 86400 * 7; on the 0 you can change the week starting from the current week.
$ts = date(strtotime('last monday'));
$ts += 0 * 86400 * 7;
$dow = date('N' , $ts);
$offset = $dow;
//the output is currently only for testing
$ts = $ts - $offset * 86400;
$week = date('W', $ts);
echo "<p>$week</p>";
for ($x=0 ; $x<7 ; $x++,$ts += 86400) {
echo '<p>' . date("d.m.Y", $ts) . '</p>' ;
}
Output to the code and current week
Try something simpler, like this?
<?php
$currentDate = date('d.m.Y', strtotime('last monday'));
for ($x=0 ; $x<7 ; $x++) {
echo '<p>' . $currentDate . '</p>' ;
$currentDate = date("d.m.Y", strtotime($currentDate . ' +1 day'));
}
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))];
I'm trying to reschedule recurring events based on the recurring method: daily, weekly or monthly.
So assuming I have this data:
$now = time();
$start_date = '01/01/2011 14:00';
$end_date = '01/01/2011 14:20';
$start_time = date('H:i', $start_date);
$end_time = date('H:i', $end_date);
$period = $end_date - $start_date; // in this case, 20 minutes
This event rescheduled daily would be:
$new_time_start = strtotime(date("m/d/Y {$start_time}", $now));
$new_time_end = $new_time_start + $period;
And monthly:
$start_day = date('d', $start_date);
$new_time_start = strtotime(date("m/{$start_day}/Y {$start_time}", $now));
$new_time_end = $new_time_start + $period;
It seems to be working, but I don't know how to do the same for weekly events :(
For example '01/01/2011' is Saturday, so I want to reschedule the event to run every Saturday (day 6 of the week according to PHP).
Any ideas?
Give this a try...
$today = date('w');
$day = date('w', $start_date);
$diff = abs(7 - ($today - $day));
$new_time_start = strtotime('+'.$diff.'days', strtotime(date("m/d/Y {$start_time}")));
$new_time_end = $new_time_start + ($end_date - $start_date);
It seems as though you're missing some strtotime( ) wrappers around your $start_date and $end_date values. I'm assuming your storing your start and end date variables as unix timestamps, so I didn't include them in my code either.
Also, just as a side note, you don't have to enter time( ) in date( ) as the second argument. If the time value is missing, it will use the current time. So you can remove all your $now instances and it will still work.
I'm not too sure I understand your question, but can't you do this?
$time = '2011-01-31';
$weekLater = date('Y-m-d', strtotime('+7 days ' . $time));
echo $time . ' -> ' . $weekLater;
Output : 2011-01-31 -> 2011-02-07
Whatever date you give it, it will give you a date 7 days in the future. Is this what you need?
Hay, i have a database holding events. There are 2 fields 'start' and 'end', these contain timestamps. When an admin enters these dates, they only have the ability to set the day,month,year. So we are only dealing with stamps containing days,months,years, not hours,minutes,seconds (hours,minutes and seconds are set to 0,0,0).
I have an event with the start time as 1262304000 and the end time as 1262908800. These convert to Jan 1 2010 and Jan 8 2010. How would i get all the days between these timestamps? I want to be able to return Jan 2 2010 (1262390400), Jan 3 2010 (1262476800) .. all the way to the end stamp. These events could cross over into different months, say May 28 to June 14.
Any ideas how to do this?
You just have to calculate the number of seconds between the two dates, then divide to get days :
$numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
Then, you can use a for loop to retrieve the dates :
$numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
for ($i = 1; $i < $numDays; $i++) {
echo date('Y m d', strtotime("+{$i} day", $smallestTimestamp)) . '<br />';
}
Again, if you don't know which timestamp is the smallest, you can use the min() function (second argument in strtotime).
I think that a quick workaround for this is to subtract the amount of a days worth of seconds from the end_stamp until you get to the start_tag.
//1 day = 86400 seconds
I would build an array of the days to use later.
EDIT (example)
$difference = 86400;
$days = array();
while ( $start_time < $end_time )
{
$days[] = date('M j Y', $end_time);
$end_time -= $difference;
}
This should cover any time frame even if its over a bunch of months.
Try this:
while($date_start <= $date_end) {
echo date('M d Y', $date_start) . '<br>';
$date_start = $date_start + 86400;
}
Hope this helps !
$d1=mktime(22,0,0,1,1,2007);
$d2=mktime(0,0,0,1,2,2007);
echo "Hours difference = ".floor(($d2-$d1)/3600) . "<br>";
echo "Minutes difference = ".floor(($d2-$d1)/60) . "<br>";
echo "Seconds difference = " .($d2-$d1). "<br>";
echo "Month difference = ".floor(($d2-$d1)/2628000) . "<br>";
echo "Days difference = ".floor(($d2-$d1)/86400) . "<br>";
echo "Year difference = ".floor(($d2-$d1)/31536000) . "<br>";
http://www.plus2net.com/php_tutorial/date-diff.php
http://www.phpf1.com/tutorial/php-date-difference.html
$daysInBetween = range($startTs, $endTs, 86400);
$secondDay = date('M d Y', $daysInBetween[1]);
/*
$thirdDay = date('M d Y', $daysInBetween[2]);
...
*/
Note that the range() function is inclusive.
**This is a very simple code for find days hours minutes and seconds in php**
$dbDate = strtotime("".$yourbdDate.""); // Database date
$endDate = time(); // current time
$diff = $endDate - $dbDate; /// diffrence
$days = floor($diff/86400); /// number of days
$hours = floor(($diff-$days*86400)/(60 * 60)); //// number of hours
$min = floor(($diff-($days*86400+$hours*3600))/60);///// numbers of minute
$second = $diff - ($days*86400+$hours*3600+$min*60); //// secondes
if($days > 0) echo $days." Days ago";
elseif($hours > 0) echo $hours." Hours ago";
elseif($min > 0) echo $min." Minute ago";
else echo "Just second ago";
Something like this?
$day = $start;
while ($day < $end) {
$day += 86400;
echo $day.' '.date('Y-m-d', $day).PHP_EOL;
}
By the way, 1262304000 is Dec 31, not Jan 1.
get the difference of two dates and divide it by 86400. abs(($date1 - $date2) / 86400) will produce the needed result