I am new to php and I need help understanding this code - php

$ep1 = mktime(19,32,56,5,10,1965);
$ep2 = mktime(4,29,11,11,20,1962);
echo($ep2);
$diff_seconds = $ep1 - $ep2;
$diff_weeks = floor($diff_seconds/604800); // 128
*$diff_seconds -= $diff_weeks * 604800;
$diff_days = floor($diff_seconds/86400);
*$diff_seconds -= $diff_days * 86400;
$diff_hrs = floor($diff_seconds/3600);
*$diff_seconds -= $diff_hrs * 3600;
$diff_mins = floor($diff_seconds/60);
*$diff_seconds -= $diff_mins * 60;
echo('<br>');
echo("Difference = $diff_weeks,$diff_days,$diff_hrs,$diff_mins");
I want to understand why the lines marked with an asterisks(*) has been done?
Thank you

Let's start the other way round.
Say you want to express the timespan (3 days) + (17 hours) + (47 minutes) + 13 seconds in seconds. That would be (without leap seconds et al)
(3*86400) + (17*3600) + (47*60) + 13
= 259200 + 61200 + 2820 13
= 323233 (=$diff_seconds)
Now let's take a look what the script does ( we skip the weeks and start with the days)
$diff_days = floor($diff_seconds/86400);
= floor(323233/86400);
= floor(3,7...);
= 3
$diff_seconds -= $diff_days * 86400;
$diff_days * 86400 = 259200 <-- see? That's the first factor in (3*86400) + (17*3600) + (47*60) + 13
those 259200 have already "been handled", so they are subtracted from the the seconds that are still to be processed. After that $diff_seconds only counts the seconds that can't be expressed as days (as integer).
The next step handles all seconds that can be expressed as (integer) hours and again subtracts them from the remaining seconds and so on and on.

Those are Assignment operators, here is a list of all operators in php.

Related

How to Round Up to the nearest 15 minutes in PHP [duplicate]

This is not a duplicate question, but involves a little understanding about time.
I need a solution to the following problem
I have a number of specifically produced times (based on a date), that need to be rounded to the nearest 15 secs:
60 secs is 1 minute
meaning a regular round, floor, ceiling is to the nearest decimal (10/5)
which doesn't help me with time.
also since I'm dealing with secs, it could be that 59:59 will be rounded up to the nearest hour: e.g. 17:59:59 should be 18:00.
example:
6:17:29 rounded to 6:17:30
6:29:55 rounded to 6:30:00
20:45:34 rounded to 20:45:30
The following code does some of the job:
$hr = date('H',($resultStr));
$mn = date('i',($resultStr));
$sc = date('s',($resultStr));
$tot = ($hr * 60 * 60) + ($mn * 60) + $sc;
$totd = $tot / (60);
$totc = ceil($totd);
$totc = $totc / 60;
$hr = floor($totc);
$mn = ($totc - $hr)*60;
$mnflr = floor($mn);
$mn2 = $mn - $mnflr;
echo "$hr:$mnflr";
This results in:
18:35:17 rounded to: 18:36 (which is wrong)
18:31:49 rounded to: 18:32 (which is wrong)
As an aside:
$secs = date('U',($resultStr));
$round = ceil ( (($secs / 60 ) * 60 ));
$newtime = date('H:i:s',($round));
produces: 18:42:58 rounded to: 18:42:58 which is also incorrect
Please and thank you in advance....
You're massively overcomplicating this, just do rounding on the Unix timestamp level:
function roundMyTime($time)
{
$time = strtotime($time);
$time = 15*round($time/15);
echo date('H:i:s', $time)."\n";
}
roundMyTime('18:35:17');
roundMyTime('18:35:27');
roundMyTime('18:35:37');
roundMyTime('18:35:47');
roundMyTime('18:35:57');
roundMyTime('18:36:07');
roundMyTime('18:36:17');
Outputs:
18:35:15
18:35:30
18:35:30
18:35:45
18:36:00
18:36:00
18:36:15
Demo here.
$seconds = ($hr * 60 + $mn) * 60 + $sc; // convert to seconds
$rounded = round($seconds/15)*15; // round
$sc = $rounded % 60; // get seconds
$mn = ($rounded - $sc) / 60 % 60; // get minutes
$hr = ($rounded - $sc - $mn * 60) / 60; // get hours
Convert the date to seconds using strtotime and then just work in seconds.
$seconds = strtotime($date);
$seconds /= 15;
$seconds = round($seconds);
$seconds *= 15;
$date = date("Y-m-d H:i:s", $seconds);

PHP round the time to the nearest 15 seconds

This is not a duplicate question, but involves a little understanding about time.
I need a solution to the following problem
I have a number of specifically produced times (based on a date), that need to be rounded to the nearest 15 secs:
60 secs is 1 minute
meaning a regular round, floor, ceiling is to the nearest decimal (10/5)
which doesn't help me with time.
also since I'm dealing with secs, it could be that 59:59 will be rounded up to the nearest hour: e.g. 17:59:59 should be 18:00.
example:
6:17:29 rounded to 6:17:30
6:29:55 rounded to 6:30:00
20:45:34 rounded to 20:45:30
The following code does some of the job:
$hr = date('H',($resultStr));
$mn = date('i',($resultStr));
$sc = date('s',($resultStr));
$tot = ($hr * 60 * 60) + ($mn * 60) + $sc;
$totd = $tot / (60);
$totc = ceil($totd);
$totc = $totc / 60;
$hr = floor($totc);
$mn = ($totc - $hr)*60;
$mnflr = floor($mn);
$mn2 = $mn - $mnflr;
echo "$hr:$mnflr";
This results in:
18:35:17 rounded to: 18:36 (which is wrong)
18:31:49 rounded to: 18:32 (which is wrong)
As an aside:
$secs = date('U',($resultStr));
$round = ceil ( (($secs / 60 ) * 60 ));
$newtime = date('H:i:s',($round));
produces: 18:42:58 rounded to: 18:42:58 which is also incorrect
Please and thank you in advance....
You're massively overcomplicating this, just do rounding on the Unix timestamp level:
function roundMyTime($time)
{
$time = strtotime($time);
$time = 15*round($time/15);
echo date('H:i:s', $time)."\n";
}
roundMyTime('18:35:17');
roundMyTime('18:35:27');
roundMyTime('18:35:37');
roundMyTime('18:35:47');
roundMyTime('18:35:57');
roundMyTime('18:36:07');
roundMyTime('18:36:17');
Outputs:
18:35:15
18:35:30
18:35:30
18:35:45
18:36:00
18:36:00
18:36:15
Demo here.
$seconds = ($hr * 60 + $mn) * 60 + $sc; // convert to seconds
$rounded = round($seconds/15)*15; // round
$sc = $rounded % 60; // get seconds
$mn = ($rounded - $sc) / 60 % 60; // get minutes
$hr = ($rounded - $sc - $mn * 60) / 60; // get hours
Convert the date to seconds using strtotime and then just work in seconds.
$seconds = strtotime($date);
$seconds /= 15;
$seconds = round($seconds);
$seconds *= 15;
$date = date("Y-m-d H:i:s", $seconds);

Formating integers in percentage in php

I have two integers that are $hours = 74 and $minutes = 20, the format I need to get them in is following: hours and minutes (without any spacing) in percentage of one hour. So in this case the final result should be 7433.
Just to make it more clear if the two numbers would be $hours = 74 and $minutes = 30, the final result should be 7450.
I have been trying to look for similar functions, but without any success.
Any help or guidance is much appreciated.
So really what you are looking for is $result = $hours . floor($minutes/60*100); ?
Or if you need the leading zeroes: $result = str_pad($hours,2,'0') . str_pad(floor($minutes/60*100),2,'0');
Save yourself the pain of coming up with code that handles cases where the value of $minutes >= 60 by using the DateTimeInterface objects. I admit, they may seem overkill in this situation, but they are very sturdy and reliable. Plus, if ever you'd want to add days, weeks, months, years or seconds to this code, the DateTimeInterface classes are already equipped for the job:
$now = new DateTime();
$comp = clone $now;
//2 identical datetime instances
//add hours + minutes to either one
$comp->add(
sprintf(
'PT%dH%dM',
$hours,
$minuts
)
);
//get difference in seconds
$diff = $comp->getTimeStamp() - $now->getTimeStamp();
//or echo, I used printf to limit the number of decimals to 2
printf(
'%.2f hours difference'
$diff/3600 //1 hour === 3600 seconds
);
Just browse the DateTime docs, and other classes/interfaces like DateInterval and others implementing the DateTimeInterface.
Just for completeness, here's how I'd set about doing this "manually"
$decimalT = $hours + floor($minutes/60) + ($minutes%60)/60
//add hours in case $minutes>= 60
//floor($minutes/60);
//get remainder minutes, converted to decimal hours
//($minutes%60)/60;
printf(
'%d hours + %d seconds == %.2f hours',
$hours,
$minutes,
$decimalT
);
Use this snippet of code:
$hours = 74;
$minutes = 20;
$totalMinutes = $hours * 60 + $minutes;
$percentage = floor(($totalMinutes * 100) / 60);
var_dump($percentage);

How to get current time elapsed percentage of today? [duplicate]

This question already has answers here:
Work out the percentage of the day that has elapsed
(5 answers)
Closed 9 years ago.
Im trying to get the current time elapsed percentage of todays time. It can be either javascript or php. How can I do that ?
Use a Date object and some arithmetic. Convert the components of the current day into a equivalent unit (such as seconds), find their sum, and divide that by the number of that unit per day.
For example, the following is a solution in JavaScript.
var d = new Date();
var pctDayElapsed = (d.getHours() * 3600 + d.getMinutes() * 60 + d.getSeconds() + d.getMilliseconds()/1000)/86400;
Note that this approach piggybacks on the browser's localization. Your result will depend on the browser's timezone.
I would use the current time to calculate the number of seconds that has elapsed today and then divide that by 86400 (the number of seconds in a day), and of course multiply that by 100 to get it from decimal to percent.
Here is an answer in PHP:
$now=time();
$today=strtotime(date("m/d/Y"));
$seconds=$now-$today;
$day=24*60*60;//seconds in a day;
$percent=$seconds/$day*100;
OR
$hours=date('G')*60*60;
$minutes=date('i')*60;
$seconds=date('s');
$day=24*60*60;//seconds in a day;
$percent=($hours+$minutes+$seconds)/$day*100
<?php
$timestamp = time();
$hours = intval(date("G", $timestamp));
$minutes = intval(date("i", $timestamp));
$seconds = intval(date("s", $timestamp));
$proportion_elapsed = ($hours * 60 * 60 + $minutes * 60 + $seconds) /
(24 * 60 * 60);
printf("%0.4F of the day has elapsed.", $proportion_elapsed);
?>
This works as long as there are 86,400 seconds in a day, which may not be true due to Daylight Savings time or leap seconds.
Round down to the beginning of the day by dividing the current time by 86400, then multiply that integer value by 86400. Then take the difference of the current time (in seconds of course) and then divide 86400 into it. Lastly, multiply by 100.
Edit: mod works more efficiently
Pseudocode:
MidnightDays = (TimeInSeconds % 86400) * 86400;
Percentage = (TimeInSeconds - MidnightDays) / 86400 * 100;

Converting MP3 duration time

I'm using my iTunes library to get data from about 1,100 mp3s and I'm running into a small issue in getting the duration of the library into minutes and seconds.
$duration = 1893642;
$minutes = bcmod(($duration / 60), 60);
$seconds = bcmod($duration, 60);
echo $minutes.":".$seconds; //returns 0:42
The problem is that this specific MP3 is actually 31:42. Any thoughts on why this isn't working?
$minutes = bcmod(($duration / 60), 60);
is taking the minutes modulo 60. Unless your track is over an hour it will always say 0.
You want it to be
$minutes = floor($duration / 60);
Try this function
function formatTime($secs) {
$times = array(3600, 60, 1);
$time = '';
$tmp = '';
for($i = 0; $i < 3; $i++) {
$tmp = floor($secs / $times[$i]);
if($tmp < 1) {
$tmp = '00';
}
elseif($tmp < 10) {
$tmp = '0' . $tmp;
}
$time .= $tmp;
if($i < 2) {
$time .= ':';
}
$secs = $secs % $times[$i];
}
return $time;
}
Not sure if the following function was available when this question was written, but as it's a question I've been asking myself so here goes.
I used the answer above:
$seconds = bcmod($row{'playtime_seconds'}, 60);
$minutes = floor($row{'playtime_seconds'} / 60);
$hours = floor($minutes / 60);
Which works for the majority of times, but there is no padding - so you can end up with 20:1 when it should be 20:01 - and it's not to good over an hour - one length comes in at length="1:70:9" - so an alternative is to use the "date" function.
<?=date("H:i:s", $duration); ?>
which returns 00:31:42 from that number of seconds
$duration_str = sprintf('%s:%02s:%02s',
floor($duration_int / 3600), // hours
floor($duration_int / 60) - floor($duration_int / 3600) * 60, // minutes
$duration_int % 60); // seconds
The *printf functions provide formatting. In this case the leading zero.
The minutes line is the most complex part, since you have to calculate the hours (duration [s] / 3600 [s/h]), then round down to integer (floor()), then multiply with 60 to transform to minutes, then subtract that from the total number of minutes (duration [s] / 60 [s/m]).
If your durations are shorter than an hour, the code is much simpler:
$duration_str = sprintf('%s:%02s', floor($duration_int / 60), $duration_int % 60);
The result is still correct for a duration greater than 59 minutes, but just not as readable (31560 minutes in the example).

Categories