I am retrieving records from a database which have separate date and time field and combining them into date time objects so that I can get the difference between start and finish date/time. I also want to calculate to total number of hours that have passed for the number of records returned.
My current code is like this
$temp_total = date_create("00:00");
while($row = mysqli_fetch_assoc($result))
{
$startDate = $row['StartDate'];
$startTime = $row['StartTime'];
$start = $startDate . " " . $startTime;
$start = new DateTime($start);
$finishDate = $row['FinishDate'];
$finishTime = $row['FinishTime'];
$finish = $finishDate . " " . $finishTime;
$finish = new DateTime($finish);
$duration = $start->diff($finish);
$temp_total->add($duration);
}
This just ends up with the times wrapping around i.e. 8hr, 12hr and 12 hrs gives me a total of 8 hours which I assume is because I am missing the added day somewhere.
Am I going about this in the right way or is there a better way of doing it?
Related
I have a job that runs every 28 days. and I want to assign it a cycle number based on a starting reference date.
e.g
1st cycle is 01/27/22. and that cycle number would be 2201.
subsequently I want to calculate the cycle number based on the current date. but for each year there could be either 13 or 14 cycles.
I've managed to figure out the number of cycles since the reference date to figure out the latest cycle date (see below)
const REF_ZERO_DATE = '01/27/2022';
const REF_ZERO_CYCLE_YEAR = "22";
const REF_ZERO_CYCLE_NUM = "01";
$today = new \DateTime("2023/12/29");
echo ("Today = ".$today->format("Y/m/d")."\n");
$ref_zero = new \DateTime(self::REF_ZERO_DATE);
echo ("ref_zero = ".$ref_zero->format("Y/m/d")."\n");
$number_of_days_since_ref_zero = $today->diff($ref_zero)->format("%a");
echo ("Number of days since ref zero = ".$number_of_days_since_ref_zero."\n");
$number_of_cycles_since_ref_zero = floor($number_of_days_since_ref_zero/28);
echo ("Number of cycles since ref zero = ".$number_of_cycles_since_ref_zero."\n");
$interval = 'P' . $number_of_cycles_since_ref_zero*28 . 'D';
echo ("Interval = ".$interval);
$date_of_lastest_cycle = date_add($ref_zero,new \DateInterval($interval));
echo ("last Cycle Date = ".$date_of_lastest_cycle->format("Y/m/d")."\n");
But my math for the cycle adjustment is missing coping with 12 or 13 cycle in a specific year.
It is not explicitly stated whether the cycle of the previous year continues into the next or not.
The scenario in which the cycles can overlap between years is more complicated, so this is assumed.
The interval count code was extracted to the following function:
function calculateIntervalCount($startDate, $endDate, $interval) {
$start = new \DateTime($startDate);
$end = new \DateTime($endDate);
$interval = new \DateInterval($interval);
$periodDays = intval($end->diff($start)->format('%a'));
$intervalDays = intval($interval->format('%d'));
return floor($periodDays / $intervalDays);
}
There are two cases when calculating the interval count of a particular year:
year of start and end are the same year
year of end is after year of start
In the first case the interval count is the same as the interval count of the whole period.
In the second case the interval count of a particular year can be calculated from the difference between the interval counts of the whole period and the period before the end year.
The following function returns the cycle number:
function calculateCycleNumber($startDate, $endDate, $interval) {
$totalCycles = calculateIntervalCount($startDate,$endDate,$interval);
$startYear = intval((new \DateTime($startDate))->format('Y'));
$endYear = intval((new \DateTime($endDate))->format('Y'));
if($startYear < $endYear) {
$endOfLastYearDate = (new \DateTime($endDate))->modify('last day of December last year')->format('Y-m-d');
$cyclesSinceEndOfLastYear = calculateIntervalCount($endOfLastYearDate, $endDate, $interval);
$yearCycle = $totalCycles - $cyclesSinceEndOfLastYear + 1;
} else {
$yearCycle = $totalCycles;
}
$yearCode = substr($endYear,-2);
$yearCycleCode = sprintf('%02d', $yearCycle);
return $yearCode . $yearCycleCode;
}
A cycle number of 2314 was obtained with the inputs provided.
echo calculateCycleNumber('01/27/2022','2023/12/29','P28D');
Note that 14 is possible in case of overlapping cycles.
You can use timestamp, where you add 28 days each time so you get the next date and so on.
Get the next timestamp
$next_date = strtotime('+28 day', $timestamp);
Convert to readable date
echo date('m/d/Y', $next_date);
I'm working on a tick based space game http://ricarion.com/ but the ticks only run between certain hours.
08:00-16:30 - run every 30 minutes via a cron job. In the nav bar at the top I want to add "Next Tick: 08:30 06/02/20" for example.
So I was thinking of creating an array:
$tick_times[] = array();
$tick_times[] = 08:00;
$tick_times[] = 08:30;
$tick_times[] = 09:00;
...
$tick_times[] = 16:30;
And then this is where I get stuck, how do I check the existing time, and then compare that against the array selecting the next future time? i.e. It's now 08:34, so the return should be 09:00?
Did you need an array or just want to calculate the next 30-minute interval?
If so this may be similar to:
Round minute down to nearest quarter hour
You do modulo division of 1800 seconds on the current time and add the answer (time remainder of time to the next interval) to the then-current time to get the next event.
<?php
$current_date = date('d-M-Y g:i:s A');
echo $current_date."\n";
$current_time = strtotime($current_date);
$frac = 1800;
$r = $current_time % $frac;
$new_time = $current_time + ($frac-$r);
$new_date = date('d-M-Y g:i:s A', $new_time);
echo $new_date."\n";
http://codepad.org/xs9lMCRQ
Get the now time format it and compare it. In your case you maybe format your $tick_time to the same format like current time.
$date = new DateTime('now');
$date = $date->format('Y-m-d H:i:s');
foreach ($tick_times as $tick_time) {
$date_added = new DateTime($tick_time);
if (strtotime($date_added) == strtotime($date)) {
//do your stuff here
}
}
I have a function where I pass total 5 parameters to it.
$Date1, $Time1, $Date2 and $Time2 and $Interval.
I first form a timestamp1 using Date1Time1, then form a timestamp2 using Date2Time2 , and then I divide these two timestamps into equal intervals of hours and then store into an associative array.
e.g.
$Date1 = 27-03-2016
$Time1 = 18:00
$Date2 = 27-03-2016
$Time2 = 21:00
Now I want to divide this time into equal time intervals of 60 mins, and then want to store into an associative array into below format.
$array = [27-03-2016 => 18:00 , 27-03-2016 => 19:00, 27-03-2016 => 20:00, 27-03-2016 => 21:00]
I have written below function in php. When I run this, the file is getting hanged forever and not responding anything and when I check the server logs then it gives
error: Maximum execution time of 30 seconds exceeded at line
$end_time = date('H-i',strtotime($end_timestamp));
As I am comparatively new to php, I am not able to understand what is going wrong.
function FindTimeSpan (&$Date1,&$Time1,&$Date2,&$Time2,&$Interval)
{
$timespan=array($Date1 => $Time1);
$timestamp1 = strtotime($Date1 . $Time1);
$timestamp2 = strtotime($Date2 . $Time2);
while( $Date1 < $Date2)
{
$start_timestamp = $timestamp1;
$end_timestamp = $timestamp2 . '+' .$Interval;
//Separating Date and Time from a timestamp
$end_date = date('Y-m-d',strtotime($end_timestamp));
$end_time = date('H-i',strtotime($end_timestamp));
//pushing value to an array
$timespan = array_merge($timespan, array($end_date => $end_time));
//setting the start value to the new end value
$timestamp1 = $end_timestamp;
}
echo 'timestamp array' . json_encode($timespan);
}
The problem you encounter is an infinite loop, which is caused by the condition $Date1 < $Date2 . You don't modify any of these two values, so the condition will always be true. Strangely, you don't use the different timestamp values that are modified, but you should.
Concerning that, you should replace $end_timestamp = $timestamp2 . '+' .$Interval; by simply $end_timestamp = $timestamp2 + $Interval ; . Using the single quotes and the point will make PHP think of this as a string operation instead of a mathematical operation. With this and using this loop condition $timestamp1 < $timestamp2, your code should stop.
As said in a comment, your array structure is impossible because you can't assign have the same key multiple times. Instead of this, you should create an array per date and pushing the different times to these arrays.
To do this, you should first fix the way you retrieve the date and time in the loop. In the following code, the call to strtotime is unnecessary as the function date requires a timestamp, so no need to convert this back to a string.
//Separating Date and Time from a timestamp
$end_date = date('Y-m-d',strtotime($end_timestamp));
$end_time = date('H-i',strtotime($end_timestamp));
You should also be consistent, the format used here for the date is not consistent with the format you gave as exemple.
Now as per your suggestions above, I have made few changes in my code as well as the requirement.
I have not decided to use Assoc array.
Instead I will divide the two stamps between equal timeintervals (assuming it is possible).
Then I am doing the simple array push of this string.
Later on once this array is form, I will parse it and take the Date and Time separate.
Now my code is not entering into infinite loop as I am comparing the two timestamps. But now, the issue is it is pushing the first value to the array but all the subsequent values are getting pushed as null
so the output I am getting from the below code is like
array[1459051200,null,null,null,......]
Below is the code
$Date1 = "2016-03-27";
$Time1 = "00:00";
$Date2 = "2016-03-30";
$Time2 = "22:00";
$Interval = '60';
FindTimeSpan ($Date1, $Time1, $Date2, $Time2, $Interval);
function FindTimeSpan (&$Date1,&$Time1,&$Date2,&$Time2,&$Interval)
{
$timestamp1 = strtotime($Date1 . $Time1);
$timespan=array();
array_push($timespan,$timestamp1);
echo 'Value of array timespan' . json_encode($timespan);
$timestamp2 = strtotime($Date2 . $Time2);
while( $timestamp1 < $timestamp2)
{
$start_timestamp = $timestamp1;
$end_timestamp = $timestamp1 + $Interval;
//pushing value to an array
array_push($timespan,$end_timespan);
//
$timestamp1 = $end_timestamp;
}
echo 'timestamp array' . json_encode($timespan);
}
This may sound like a dumb question, but how can I convert the time between two dates to a percent?
I am using this Jquery plugin: http://tinacious.github.io/goalProgress/
The script on page that calculates the percent is:
$('#timerGoal').goalProgress({
goalAmount: 100,
currentAmount: 40,
textBefore: '',
textAfter: '% Completed.'
});
Where it says goalAmount: I'd like that to remain at 100, but where it says currentAmount: 40, I'd somehow like to find the difference in percentage between two days, I know I'd have to set a start date, current date, and end date to find a percentage.
I'm certain part of the code would have to be:
$startDate = '01/01/2015 12:00:00';
$currentDate = date('d/M/Y H:i:s');
$endDate = '02/15/2015 12:00:00';
Finding the difference in two dates is fairly easy, but it's the third date thing I cannot grasp, especially to make it a percentage.
Any ideas?
I was thinking something along the lines of:
[Taken from: How to find the difference in days between two dates ]
$daylen = 60*60*24;
$date1 = '2010-03-29';
$date2 = '2009-07-16';
echo (strtotime($date1)-strtotime($date2))/$daylen;
But everything I read on is two dates not three.
Here is what I've come up with.
It's not calculating percentages yet, but it's something to possibly go off of:
$startDate = '08/01/2015 12:00:00';
$currentDate = date('d/M/Y H:i:s');
$endDate = '09/01/2015 12:00:00';
$startDate =str_replace(array(':', '/', ' '), '', $startDate);
$currentDate =str_replace(array(':', '/', ' '), '', $currentDate);
$endDate =str_replace(array(':', '/', ''), ' ', $endDate);
$mainPercent = $endDate - $startDate;
$actualPercent = $endDate - $currentDate;
$displayPercent = $actualPercent/$mainPercent * 100;
echo $displayPercent;
With todays date being 08/07/2015 I am getting 901.2015119993 which is obviously not a percent, but it's a start.
Working Solution:
$startDate = strtotime('08/01/2015 12:00:00');
$currentDate = time(date('d/M/Y H:i:s'));
$endDate = strtotime('09/15/2015 12:00:00');
$dateDivideBy = $endDate - $startDate;
$dateDivide = $currentDate - $startDate;
$divideProduct = $dateDivide / $dateDivideBy;
$datePercent = round($divideProduct * 100);
echo $datePercent;
With this working code and todays date being 08/07/2015 the value of $datePercent is 14.
The difference between two times, by itself, really can't be converted to a percentage. It's just a period of time. In order to figure out what percentage is complete, you would need to know how long the entire goal is supposed to take (an estimated time, I assume.) Then you can figure out the percentage like this:
ElapsedTime / TotalTime * 100
The total time would be End Date - Start Date, and the elapsed time would be now - start date.
Rather than using string functions to manipulate the dates, it would be better to use DateTime functions.
$startDate = '08/01/2015 12:00:00';
$endDate = '09/01/2015 12:00:00';
$startDate = new DateTime($startDate);
$currentDate = new DateTime(); // defaults to now
$endDate = new DateTime($endDate);
$totalTime = $endDate->diff($startDate)->format('%a');
$elapsedTime = $currentDate->diff($startDate)->format('%a');
// diff returns a DateInterval object; calling its format method
// with %a returns the number of days in the interval
$percent = ($elapsedTime / $totalTime) * 100;
I believe this is your desired outcome, where result is the resulting percent difference between start_actual_time and percent_time:
var percent_time= new Date("01/17/2013 11:20");
var start_actual_time = new Date("01/17/2012 11:20");
var end_actual_time = new Date("01/17/2014 11:20");
var range = end_actual_time - start_actual_time;
var diff = end_actual_time - percent_time;
var result = (diff / range)*100;
In this example, start_actual_time and percent_time are 40% different.
I have been looking through all the previous questions which similar to this question unfortunately non of them work for me.
I am trying to get the number of weeks between two dates.
$result = mysqli_query($con,"SELECT * FROM `test` WHERE `DateofTest` BETWEEN '" .
$startDate . "' AND '" . $endDate . "' ") or die ("Error: ".mysqli_error($con));
$startDate = $_POST['start'];
$endDate = $POST['end'];
Suppose my start date is 01/12/2014 and end date is 31/12/2014 so 4 weeks.
Here is my code
$startDate ="2014-12-01";
$endDate ="2014-12-31";
$days=($startDate - $endDate);
echo $days;
$weeks=($days / 7);
echo $weeks;
I am getting 0 result for each days and weeks.
Any ideas please.
Thanks
Something like this using the date time object will work
$d1 = new DateTime("2014-12-01");
$d2 = new DateTime("2014-12-31");
$difference_in_days = $d1->diff($d2)->days;
echo "Diff in Weeks = ".$difference_in_days/7;
You can't calculate strings. You need to convert them to dates.
You can do something like:
function get_number_of_weeks($startDate, $endDate) {
// use strtotime and substract the end date from the start date, not the otherway around
$days = strtotime($endDate) - strtotime($startDate);
// devide by seconds / hours and weeks
$weeks = $days / 3600 / 24 / 7;
// floor the amount of weeks.
return floor($weeks);
}
echo get_number_of_weeks("2014-12-01", "2014-12-31");
You cannot compare date strings and expect to get the difference.
You should use the DateTime class to compare two datetime values:
$startDate = new DateTime("2014-12-01");
$endDate = new DateTime("2014-12-31");
$diff = $startDate->diff( $endDate )->format('%d');
$weeks = floor($diff/7);
format method can return a difference in a number of ways like years/months/days/hours/minutes/seconds. More here
Try this..
<?php
$d1 ="2014-12-01";
$d2 ="2014-12-31";
$diffweek = abs(strtotime($d1) - strtotime($d2)) / 604800;
echo round($diffweek); or echo intval($diffweek);
?>