when start date ex:6/1/2016 and
end date ex : 8/1/2016
It is the process by computational days, so the process is the result of 3 days
6/1/2016 day
7/1/2016 day
8/1/2016 day
This is wrong .
Must be 6/1/2016 to 7/1/2016 day and 7/1/2016 to 8/1/2016 day
so the sum 2 day not 3 days
$start_date = strtotime($date_from);
$end_date = strtotime($date_to);
$datetime1 = date_create($date_from);
$datetime2 = date_create($date_to);
$interval = date_diff($datetime1, $datetime2);
$cs_booking_days = $interval->days;
// Loop between timestamps, 24 hours at a time
$total_price = '';
$adult_price = 0;
$pricings = get_option('cs_price_options');
$cs_offers_options = get_option("cs_offers_options");
$pricings_array = $pricings[$post_id];
if (isset($pricings[$post_id]['cs_plan_days'])) {
$cs_sp_days = $pricings[$post_id]['cs_plan_days'];
}
$pricing_data = array();
$brk_counter = 0;
$total_orignal = 0;
$price['total_price'] = 0;
$flag = false;
for ($i = $start_date; $i <= $end_date; $i = $i + 86400) {
$total_days++;
$brk_counter++;
$thisDate = date('Y-m-d', $i); // 2010-05-01, 2010-05-02, etc
$day = strtolower(date('D', strtotime($thisDate)));
$adult_price = $pricings_array['cs_pricing_branches']['adult_' . $day . '_price'][0];
$adult_temp_price = $adult_price != '' ? $adult_price : 0;
$adult_price = $adult_temp_price;
$to_check_date = strtotime(date('Y-m-d', $i));
<?php
// your code goes here
$startTimeStamp = strtotime("2016/01/06");
$endTimeStamp = strtotime("2016/01/08");
$timeDiff = abs($endTimeStamp - $startTimeStamp);
$numberDays = $timeDiff/86400; // 86400 seconds in one day
// and you might want to convert to integer
$numberDays = intval($numberDays);
echo $numberDays;
http://ideone.com/8miDiP
Back to your code => Replace
for ($i = $start_date; $i <= $end_date; $i = $i + 86400) {
this
for ($i = $start_date; $i < $end_date; $i = $i + 86400) {
<= is problem, type <
EXPLANATION
06.01 - 08.01
First loop:
$i = 06.01 and <= 08.01
Second loop:
$i = 07.01 and <= 08.01
Third loop:
$i = 08.01 and <= 08.01 - STILL TRUE, so SUM = 3 days
After change
First loop:
$i = 06.01 and < 08.01
Second loop:
$i = 07.01 and < 08.01
Third loop:
$i = 08.01 and < 08.01 - NOW FALSE, so SUM = 2 days
Related
I have a issue with the return of my multidimensional arrays. For some reason two of them are returned as objects and not arrays when the initialization is fairly the same.
I'm creating a time card and would like to return the % of time an employee was late and on time in a month for the total of past 12 months.
Code:
$eID = $_GET['showLatencyValues'];
//get the last 12 months
$month = time();
for ($i = 1; $i <= 12; $i++) {
$month = strtotime( date( 'Y-m-01' )." -$i months");
$months[] = date("r", $month);
}
$ii = 0;
for($i = 11; $i >= 0; $i--) {
$m = explode(" ", $months[$i]);
$result = $m[2].'. '.$m[3];
$last12months[$ii][0] = $ii;
$last12months[$ii][1] = $result;
$ii++;
}
//set the correct dates
$f = explode(" ", $last12months[0][1]);
$t = explode(" ", $last12months[11][1]);
$from = $f[1].'-'.letterToNumeric($f[0]).'-01';
$days = cal_days_in_month(CAL_GREGORIAN, letterToNumeric($t[0]), $t[1]); // 31
$to = $t[1].'-'.letterToNumeric($t[0]).'-'.$days;
//get the information from database
$sql = mysqli_query($conn, "SELECT DISTINCT `timeIn`,`date` FROM `clock` WHERE `eID`='$eID' AND date BETWEEN '$from' AND '$to'");
$res = mysqli_fetch_array($sql);
$break = explode("-", $res['date']);
$month = $break[1]; //set current month to
$countMonth = 0; //month number
$count = 0; //day number
$sql = mysqli_query($conn, "SELECT min(timeIn) as timeIn,`date` FROM `clock` WHERE `eID`='$eID' AND date BETWEEN '$from' AND '$to' group by date");
while($res = mysqli_fetch_array($sql)) {
$break = explode("-", $res['date']); //get the current selected month
if($month != $break[1]) { //check if current month is the same as selected by database if not then
$countMonth++; //add current month number
$count = 0; //set current day number to 0
$month = $break[1]; //set the current month to whatever is selected by database
}
$timeInData[$countMonth][$count] = $res['timeIn'];
$count++;
}
$count = 0;
for($i = 0; $i < count($timeInData); $i++) {
for($s = 0; $s < count($timeInData[$i]); $s++) {
$firstShift_timeIn = setting($conn, 'timeIn1');
$secondShift_timeIn = setting($conn, 'timeIn2');
$lateConsideredAfter = setting($conn, 'lateAfter');
$actualTime = date("H:i", strtotime($timeInData[$i][$s]));
if($res_shift['shift'] == 1) $considerLateTime = date("H:i", strtotime($firstShift_timeIn." +".$lateConsideredAfter." minutes"));
else $considerLateTime = date("H:i", strtotime($secondShift_timeIn." +".$lateConsideredAfter." minutes"));
if($actualTime > $considerLateTime) $late[$count]++;
else $onTime[$count]++;
}
$count++;
}
if($count != 12) {
$amt = (12-($count))-1;
for($w = 0; $w <= $amt; $w++) {
$onTimeYesData[$w][0] = $w;
$onTimeYesData[$w][1] = 0;
$onTimeNoData[$w][0] = $w;
$onTimeNoData[$w][1] = 0;
}
}
for($q = ($amt+1); $q <= 11; $q++) {
$total = $onTime[($q-($amt+1))] + $late[($q-($amt+1))];
$onTimeYesData[$q][0] = $q;
$onTimeYesData[$q][1] = round(($onTime[($q-($amt+1))]/$total)*100);
$onTimeNoData[$q][0] = $q;
$onTimeNoData[$q][1] = round(($late[($q-($amt+1))]/$total)*100);
}
$response[1] = $last12months;
$response[0] = $onTimeYesData;
$response[2] = $onTimeNoData;
echo json_encode($response[1])."\n\n".json_encode($response[0])."\n\n".json_encode($response[2]);
Output that I receive:
[[0,"Mar. 2019"],[1,"Apr. 2019"],[2,"May. 2019"],[3,"Jun.
2019"],[4,"Jul. 2019"],[5,"Aug. 2019"],[6,"Sep. 2019"],[7,"Oct.
2019"],[8,"Nov. 2019"],[9,"Dec. 2019"],[10,"Jan. 2020"],[11,"Feb.
2020"]]
{"1":[1,90],"2":[2,100],"3":[3,96],"4":[4,91],"5":[5,95],"6":[6,95],"7":[7,100],"8":[8,92],"9":[9,95],"10":[10,89],"11":[11,88]}
{"1":[1,10],"2":[2,0],"3":[3,4],"4":[4,9],"5":[5,5],"6":[6,5],"7":[7,0],"8":[8,8],"9":[9,5],"10":[10,11],"11":[11,13]}
I expect the two other outputs to be in the same format as the first one.
Any help is very much appreciated. Thanks.
I'm trying to get the count for weekdays and weekends between two dates but for weekends I'm getting 0 value .
Here is the code :
<?php
function daysCount($startDate, $endDate)
{
$weekdayCount = $weekendCount =0;
$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
for ($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24))
{
if (date("N", $i) <= 5)
{
$weekdayCount = $weekdayCount + 1;
}
if (date("N", $i) == 6 && $i%7 == 0 )
{
$weekendCount = $weekendCount + 1;
}
}
return array('weekdayCount' => $weekdayCount, 'weekendCount' => $weekendCount);
}
$startDate = "2017-07-03";
$endDate = "2017-07-10";
$days = daysCount($startDate, $endDate);
print_r($days);
?>
And this is the demo link demo updated
Can somebody help me to get the weekendCount ?
Maybe this way, base on the demo link:
function number_of_working_days($startDate, $endDate)
{
$workingDays = 0;
$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
for ($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24)) {
if (date("N", $i) <= 5) $workingDays = $workingDays + 1;
}
return $workingDays;
}
function number_of_weekend_days($startDate, $endDate)
{
$weekendDays = 0;
$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
for ($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24)) {
if (date("N", $i) > 5) $weekendDays = $weekendDays + 1;
}
return $weekendDays;
}
$startDate = "2016-11-01";
$endDate = "2016-11-30";
$workingDays = number_of_working_days($startDate, $endDate);
$weekendDays = number_of_weekend_days($startDate, $endDate);
echo "Total number of working days between $startDate and $endDate is: " . $workingDays . " day(s).";
echo "Total number of weekend days between $startDate and $endDate is: " . $weekendDays . " day(s).";
I want a PHP for loop that goes to certain number say "23" and again starts from "0".
Actually I want for the listing of time in 24 hours format and suppose a case is: I have to show time from 9 AM to 2 AM (i.e 9,10,11,12,13,14,...........23,0,1,2)
$i= range(0,23);//it doest work as it creates an array containing a range of elements
$start_time = 9;
$end_time = 2;
for ($i = $start_time ; $i<= $end_time ; $i++)
{
echo $i;
}
Please suggest a way.
Since your use-case mentions time-sensitive information you might be best off learning to use the date and strtotime functions (or the DateTime classes):
$time = strtotime('2013-09-14 02:00');
$endTime = strtotime('2013-09-15 09:00');
while ($time <= $endTime) {
echo date('Y-m-d H:i:s', $time)."\n";
$time = strtotime('+1 hour', $time);
}
// 2013-09-14 02:00:00
// 2013-09-14 03:00:00
// 2013-09-14 04:00:00
// 2013-09-14 05:00:00
// etc.
$start_time = 9;
$end_time = 2;
$end_time += ($end_time < $start_time) ? 24 : 0;
for ($i = $start_time; $i<= $end_time; $i++) {
echo ($i % 24);
}
Reason, the line $end_time += ($end_time < $start_time) ? 24 : 0; checks to see if the end time is less than the start time (which we assume means the next day), and if it is, it adds 24 hours to it. The line $i %24 is the modulus operator which will divide the number and give the remainder, so if it's 25, it will give you 1 back. Note that all hours being worked with will need to be in 24 hour format to use this method.
You can use the modulo to deduct 24 from numbers when they're greater than 23.
$start_time = 9;
$end_time = 2;
for( $i = $start_time; $i % 24 != $end_time; $i++ )
{
echo $i % 24;
}
Note that you need to be careful with this; if $end_time is greater than 23 it will be stuck in an infinite loop.
Try this its working
<?php
$i= range(0,23);//it doest work as it creates an array containing a range of elements
$start_time = 9;
$end_time = 2;
$max_value=23;
if($start_time>=$end_time)
{
for ($i = $start_time ; $i<= $max_value; $i++)
{
echo $i;
}
for($i=0; $i<=$end_time; $i++)
{
echo $i;
}
}
else
{
for ($i = $start_time ; $i<= $max_value; $i++)
{
echo $i;
}
}
?>
Again late to Answer , Try this
<?php
$start_time = 20;
$end_time = 10;
$maxTime=23;
$minTime=0;
for ($i = $minTime ; $i<=$maxTime - abs($start_time-$end_time) ; $i++)
{
$validTime= $start_time + ($i % $maxTime);
$validTime= $validTime>$maxTime?$validTime-$maxTime:$validTime;
echo $validTime;
}
?>
Check this out. Hope this helps
$start_time = 9;
$end_time = 2;
$flag = 1;
while($flag) [
echo $start_time . "<br>";
$start_time++;
if ($start_time == 23) { $start_time = 0; }
if ($start_time == $end_time) {
$flag = 0;
}
}
Does it have to be a for loop?
You can try this
function hours($range, $recurse = true, $end = array())
{
foreach($range as $time)
{
echo $time . "\n";
}
if($recurse && sizeof($end) > 0)
{
$range = range($end['start'], $end['end']);
hours($range, false);
}
}
$range = range(9,23);
$end = array('start' => 0, 'end' => 2);
hours($range, true, $end);
Maybe better to use date:
$hours = 8;
for ($i=1; $i<=8; $i++){
echo 'TIME1 = '.date("Y-m-d H:i:s",mktime (20+$i,15,18,06,05,2013)).'<br>';
}
or better DateTime class:
$date = DateTime::createFromFormat('Y-m-d H:i:s', '2013-06-05 20:15:18');
$hours = 8;
for ($i=0; $i<8; $i++){
$date->add(new DateInterval('PT1H'));
echo 'TIME2 = '.$date->format('Y-m-d H:i:s').'<br>';
}
Thanks everybody for your help, using your ideas I got my work done.
Here is the code that actually worked.
$start_time = some_dynamic_value;
$end_time = some_dynamic_value;
$i= $start_time;
$flag = false;
if($start_time > $end_time)
$flag = true;
while ($i <= $end_time || $flag)
{
echo $i;
if($i == 24)
{
$i= 0;
$flag = false;
}
$i++;
}
I'm trying to get an array of dates to populate a calendar and I'm facing an the issue of infinite loop.
Here is the code:
$month_length = 1;
$day_begin = 9;
$day_end = 19;
$event_interval = 15;
$date = new DateTime();
$today_date_name = $date->format("D");
$today_date_number = $date->format("j");
$today_date_month = $date->format("M");
$today_date_month_number =$date->format("n");
$today_date_year = $date->format("Y");
for ($length = 0; $length <= ($month_length - 1); $length++)
{
$date->modify("+$length month");
$current_date_name = $date->format("D");
$current_date_number = $date->format("j");
$current_date_month = $date->format("M");
$current_date_month_number = $date->format("n");
$current_date_year = $date->format("Y");
//calculate the length of the month
$current_month_length = cal_days_in_month(CAL_GREGORIAN, $current_date_month_number, $current_date_year);
if($current_date_month_number != $today_date_month_number){
// if we are not in the current month, start the second loop
// the first of the month.
$current_date_number = 1;
}
for($current_date_number; $current_date_number <= $current_month_length; $current_date_number++)
{
$date->setDate($current_date_year, $current_date_month_number, $current_date_number);
//set the ending before because of the loop;
//set the ending of the day
$date->setTime($day_end, 0, 0);
//get the timestamp of beginning
$ending_timestamp = $date->format("U");
//set the beginning of the day
$date->setTime($day_begin, 0, 0);
//get the timestamp of beginning
$beginning_timestamp = $date->format("U");
$day_length = $ending_timestamp - $beginning_timestamp;
//60 seconds for 1min
$interval = 60 * $event_interval;
for($the_timestamp = 0; $the_timestamp <= $day_length ; $the_timestamp + $interval)
{
$current_timestamp = $beginning_timestamp + $the_timestamp;
$date->setTimestamp($current_timestamp);
$final_array[$current_date_year][$current_date_number . " " . $current_date_month][$current_timestamp] = $date->format("H : i");
}
}
}
the last "for" loop ends with an infinite loop and so reaches the maximum execution time, it must be done under 30s.
Your code is far too complex, you are doing something wrong. The following will generate an array representing a whole year:-
$start = new \DateTime('1st January');
$end = new \DateTime('31st December');
$interval = new \DateInterval('P1D');
$period = new \DatePeriod($start, $interval, $end->modify('+ 1 day'));
$year = array();
foreach($period as $day){
$year[$day->format('M')][(int)$day->format('d')] = $day->format('D');
}
var_dump($year);
See it working
for($the_timestamp = 0; $the_timestamp <= $day_length ; $the_timestamp + $interval){
//Stuff
}
This loop doesn't increment $the_timestamp which is why it loops forever.
The last part should be $the_timestamp += $interval which is equivalent to $the_timestamp = $the_timestamp + $interval.
I'm struggling with how to build the following:
result I need:
$months = array();
$months
(
month[0] = '2011-10-1'
month[1] = '2011-11-1'
month[2] = '2011-12-1'
month[3] = '2012-1-1'
)
from the following variables:
$date = '2011-10-1';
$numberOfMonths = 4;
Any help would be appreciated.
Thanks, guys... all of these solutions work. I accepted the one that works best for my usage.
You can do it using a simple loop and strtotime():
$date = '2011-10-1';
$numberOfMonths = 4;
$current = strtotime($date);
$months = array();
for ($i = 0; $i < $numberOfMonths; $i++) {
$months[$i] = date('Y-n-j', $current);
$current = strtotime('+1 month', $current);
}
$date = DateTime::createFromFormat('Y-m-j', '2011-10-1');
$months = array();
for ($m = 0; $m < $numberOfMonths; $m++) {
$next = $date->add(new DateInterval("P{$m}M"));
$months[] = $next->format('Y-m-j');
}
<?php
function getNextMonths($date, $numberOfMonths){
$timestamp_now = strtotime($date);
$months[] = date('Y-m-d', $timestamp_now);
for($i = 1;$i <= $numberOfMonths; $i++){
$months[] = date('Y-m-d', (strtotime($months[0].' +'.$i.' month')));
}
print_r($months);
}
getNextMonths('2011-10-1', 4);
Working demo
You could use split on date and then a for loop over the month. The trick is to use something like
$newMonth = ($month + $i) % 12 + 1;
(% is called modulo and does exactly what you want.)