I have two times in two different formats that I need to add together:
A post time '12:30'
A user submitted number (to indicate a time duration) Ex: 1 = hour, 0.5 = 30 minutes
I need to add both together to get the total, Ex: 12:30 + 0.5 = 14:00.
I am stuck currently and am missing something simple, could anyone give me a pointer?
Code:
// Get Post Time
$post_date = strtotime(get_the_date('G:i'));
// Get User Submitted Number
$post_duration = strtotime($course_hours);
// Add Together
$output = $post_date + $post_duration;
// Output
echo 'Start Time: '.get_the_date('G:i') .' - ';
// Fail Here
echo 'End Time: '. date('G:i', $output);
$add = 0.5 * 60 ; // convert posted hours to minutes
$post_time = "12:30";
echo $post_date = date('G:i',strtotime("+ $add minutes" , strtotime($post_time)));
Related
I'm creative a forum and want to calculate the frequency of new posts per day. So, each post has timestamps:
$post_1 = 1526083200;
$post_2 = 1524083200;
$post_3 = 1523083200;
$post_4 = 1522083200;
What calculation do I do to find out how often posts are submitted per day. Example final output:
echo 'Every '. $frequency .' day(s)';
You can maybe try something like this :
$post_1 = 1526083200;
$post_2 = 1524083200;
$post_3 = 1523083200;
$post_4 = 1522083200;
// I add all the value in an array then sort the array to get the min and max value
$date_array = [$post_1, $post_2, $post_3, $post_4];
sort($date_array);
// Now I can select the min date and the max date
$min_date = $date_array[0];
$max_date = $date_array[count($date_array) - 1];
// I calculate the diff to get the number of day during this period
$datediff = $max_date - $min_date;
// I divide this value with the number or article post during this period
$frequency = $datediff / count($date_array);
// Now I transform this value in number of day
$frequency = round($frequency / (60 * 60 * 24));
With your example, this is what you got :
Number of articles : 4
Min date : 2018-03-26
Max date : 2018-05-12
Number of day of the period : 46
Frequency : 12
That sound good for me with those value , one article every 12 days.
Is it what you are looking for?
assuming you want the general frequency:
frequency = 1 / period
period = average time between two posts = time between oldest and newest post / number of post - 1
time between oldest and newest post = newest post - oldest post
In your example:
$post_1 = 1526083200;
$post_2 = 1524083200;
$post_3 = 1523083200;
$post_4 = 1522083200;
time between oldest and newest posts = 1526083200 - 1522083200 = 4000000 seconds = 46,2962963 Days
period = 46,2962963/3 = 15.4320987667 Days (There is on average 15 days between two posts)
frequency = 1 / 15.4320987667 = 0.06479999999 (There is on average one post each 0.0648 Days)
From my user table I retrieve an integer (e.g. 60) which is supposed to be the maximum of minutes that the user may use for his total break time.
When the user enters and exits his break, it's reorded in another table like this:
$timestamp = date("Y-m-d H:i:s");
$sql = "INSERT INTO user_log (username, in_pause, action_time)
VALUES ('$username', 1, '$timestamp')";
Where I write 1 for when he enters and 0 for when he exits.
I calculate how long the user's break lasted from those two recorded timestamps like this (after my sql query i put the timestamps into $zeiten[]):
$pause_start = new DateTime($zeiten[1]);
$pause_ende = new DateTime($zeiten[0]);
$pause_diff = $pause_start->diff($pause_ende);
This works fine so far. But I can't seem to find a way to manipulate the integer (here 60) from my user table so that I can subtract my $pause_diff from it. I want to be able to calculate the break time that the user has left.
Thanks!
Try this code:
<?php
$max_minutes = (int) 60;
$zeiten[1] = '2000-01-01 10:00:00';
$zeiten[0] = '2000-01-01 10:10:00';
$pause_start = new DateTime($zeiten[1]);
$pause_ende = new DateTime($zeiten[0]);
$pause_diff = $pause_start->diff($pause_ende);
//$pause_diff->format('Y-m-d H:i:s');
$pause_diff_minutes = $pause_diff->format('%i');
//var_dump((int) $pause_diff_minutes);
echo 'the differnece is: ' . ($max_minutes - (int) $pause_diff_minutes) . ' minutes';
The result is:
the differnece is: 50 minutes
You can play with the code here
Updated code showing how you could subtract also seconds from the 60 minutes:
<?php
$max_minutes = (int) 60;
$max_minutes_seconds = $max_minutes * 60; // max_minutes to seconds
$zeiten[1] = '2000-01-01 10:00:00';
$zeiten[0] = '2000-01-01 10:10:05';
$pause_start = new DateTime($zeiten[1]);
$pause_ende = new DateTime($zeiten[0]);
$pause_diff = $pause_start->diff($pause_ende);
$pause_diff_minutes = $pause_diff->format('%i');
$pause_diff_seconds = $pause_diff->format('%s');
// sum total diff in seconds
$total_pause_seconds = ((int) $pause_diff_minutes * 60) + $pause_diff_seconds;
$total_diff_seconds = ($max_minutes_seconds - $total_pause_seconds);
echo 'the differnece is: ' . $total_diff_seconds . ' seconds' . "\n";
echo 'which is: ' . floor($total_diff_seconds / 60) . ' minutes and ' . $total_diff_seconds % 60 . ' seconds';
The result is:
the differnece is: 2995 seconds
which is: 49 minutes and 55 seconds
play with it here
I don't know if this will make sense but I would like to get the time from percentage of a value, I have the following as an input example:
mypercentage = 50;
mytime = "00:59:59"
As you can see, my time is 60 minutes (1 hour) and 50% of that is 30 minutes therefore from the 2 above inputs I would like to get the following output
(50 % of an hour):
00:30:00
Explode the string into an array
Convert the 3 elements into numbers
Find the value in seconds. $array[0]*3600+$array[1]*60+$array[2]
Calculate the time from the value in step 3 and the percentage.
Use /3600 and /60 to find the hour and minutes and %60 to find the seconds.
put the values in an array and implode to a string, or just use
$hour.":".$minute.":".$second
Thank you all who responded, I found this solution to work:
$mypercentage = 50;
$mytime = '00:59:59';
$totaltime = date('H:i:s', strtotime($mytime));
$seconds = strtotime("1970-01-01 $totaltime UTC");
$per_seconds = ($mypercentage / 100) * $seconds;
$time_from_percentage = gmdate("H:i:s", $per_seconds);
echo $time_from_percentage;
Im trying to solve this since two days without success...
First of all my code:
PHP-Version: 5.4
SCRIPT1:
query = "SELECT start, end FROM timetable WHERE ........";
$result = mysql_query($query, $db) or die(mysql_error());
$sqlarr = mysql_fetch_array($result, MYSQL_ASSOC);
$times = array($sqlarr['start'], $sqlarr['end']);
$calced = strtotime($times[1]) - strtotime($times[0]);
$total = date("H:i:s", $calced-3600); //<-- -3600 Fixed it
echo "<br>Total: ".$total;
The start and end times are in format 00:00:00. Everytime this script calculates it appends 1 hour to the result. So if im going for a result of 5 minutes i´ll get 01:00:05.
Why???
This one is even more strange.
SCRIPT2:
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo " | ".$row['total']."<br>";
$add += strtotime($row['total']);}
echo $add;
The first script calculates the total time from start to end. The second one should get the total-times out of the db and calculate the sum of all entries. For every entry the second script substracts 2 hours.
Example:
Database => Start = 12:32:00 End = 12:32:15
Script1 Result1 = 01:00:15 (Where is this extra hour coming from?) FIXED
Every Result1 is stored in the same table(db). Script2 is loading all this rows and handling them by a while-loop.
According to how many entries there are the script subtracts serval hours.
0 Entries => Total: 00:00:00
1 Entry => Total: 23:00:xx
2 Entries => Total: 20:00:xx
3 Entries => Total: 18:00:xx
4 Entries => Total: 16:00:xx
So, with 2 entries it continues subtracting 2 hours from every total-calculation which isnt correct, abviously.
Thanks to you guys. Using DateTime made this simple and bugfree!
I am not really sure of your case with using strtotime function, but I've tried this solution and it worked, if you have php > 5.2:
You can use the DateTime class and date_diff function to get the difference date, read more here: http://us3.php.net/manual/en/datetime.diff.php
Example:
// start date
$start = new DateTime("09:23:38");
// end date
$end = new DateTime("09:23:54");
// calculate difference
$calc = date_diff($start, $end);
// prints 00:00:16
echo $calc->format('%h:%i:%s');
According to this answer It seems like by default date starts from 1:00:00 so you could subtract your date by 3600 seconds (1 hour) like this:
$start = strtotime("11:23:38");
$end = strtotime("11:23:54");
$calc = $end - $start;
echo date('H:i:s', $calc - 3600);
The problem comes from :
echo date('H:i:s', $calc);
$start - $end gives you the expected value in seconds. It depends on what value date('H:i:s', 0) is. On my computer, it's not midnight on 1 jan. 1970 !
This link as example : http://sandbox.onlinephpfunctions.com/code/81e56cbce16f6dacfedd2cb376b946a540bce36d.
You might consider substracting date('H:i:s', 0) from your result to get correct value.
Or using DateTime as #Ben Beri suggested you to do as it is (I think) the best way to do.
Suppose the target time is 4.30 pm and the current time is 3.25 pm , how will i calculate the minutes remaining to reach the target time ? I need the result in minutes.
session_start();
$m=30;
//unset($_SESSION['starttime']);
if(!$_SESSION['starttime']){
$_SESSION['starttime']=date('Y-m-d h:i:s');
}
$stime=strtotime($_SESSION['starttime']);
$ttime=strtotime((date('Y-m-d h:i:s',strtotime("+$m minutes"))));-->Here I want to calcuate the target time; the time is session + 30 minutes. How will i do that
echo round(abs($ttime-$stime)/60);
Krishnik
A quick calculation of the difference between two times can be done like this:
$start = strtotime("4:30");
$stop = strtotime("6:30");
$diff = ($stop - $start); //Diff in seconds
echo $diff/3600; //Return 2 hours. Divide by something else to get in mins etc.
Edit*
Might as well add the answer to your problem too:
$start = strtotime("3:25");
$stop = strtotime("4:30");
$diff = ($stop - $start);
echo $diff/60; //Echoes 65 min
Oh and one more edit:) If the times are diffent dates, like start is 23:45 one day and end is 0:30 the next you need to add a date too to the strtotime.