smarty, 1000 to 1K , 1000000 to 1M - smarty

I'm far from a programmer, just a webmaster and I've been trying to figure out how to get 1K instead of 1000.
After a long time I finally had something working:
function insert_viewer_count_format( $options ) {
return ($options['count'] >= 10 ? number_format($options['count'] / 1000, 2) . 'K' : 10);}
But.. now it's only showing up K, so 10 becomes 0.01K which is kinda silly..
Anybody knows how I can include an if statement in there? I'd also like 1M instead of 1000000.
Thanks a lot in advance, I really appreciate helping me on this!

Try this:
$options['count'] = 4000;
if ($options['count'] >= 1000 && $options['count'] % 1000 == 0 ) {
$result = ($options['count']/1000) . 'K';
} else {
$result = number_format($options['count'] / 1000, 2);
}

Related

Getting non overlapping between two dates with Carbon

UseCase: Admin assigns tasks to People. Before we assign them we can see their tasks in a gantt chart. According to the task assign date and deadline, conflict days (overlap days) are generated between tasks.
I wrote this function to get overlapping dates between two dates. But now I need to get non overlapping days between two dates, below is the function I wrote.
$tasks = Assign_review_tasks::where('assigned_to', $employee)
->where('is_active', \Constants::$REVIEW_ACTIVE)
->whereNotNull('permit_id')->get();
$obj['task'] = count($tasks);
// count($tasks));
if (count($tasks) > 0) {
if (count($tasks) > 1) {
$start_one = $tasks[count($tasks) - 1]->start_date;
$end_one = $tasks[count($tasks) - 1]->end_date;
$end_two = $tasks[count($tasks) - 2]->end_date;
$start_two = $tasks[count($tasks) - 2]->start_date;
if ($start_one <= $end_two && $end_one >= $start_two) { //If the dates overlap
$obj['day'] = Carbon::parse(min($end_one, $end_two))->diff(Carbon::parse(max($start_two, $start_one)))->days + 1; //return how many days overlap
} else {
$obj['day'] = 0;
}
// $arr[] = $obj;
} else {
$obj['day'] = 0;
}
} else {
$obj['day'] = 0;
}
$arr[] = $obj;
start_date and end_date are taken from database,
I tried modifying it to,
(Carbon::parse((min($end_one, $end_two))->add(Carbon::parse(max($start_two, $start_one))))->days)->diff(Carbon::parse(min($end_one, $end_two))->diff(Carbon::parse(max($start_two, $start_one)))->days + 1);
But it didn't work, in simple terms this is what I want,
Non conflicting days = (end1-start1 + end2-start2)- Current overlapping days
I'm having trouble translate this expression . Could you help me? Thanks in advance
before trying to reimplement complex stuff I recommend you take a look at enhanced-period for Carbon
composer require cmixin/enhanced-period
CarbonPeriod::diff macro method is what I think you're looking for:
use Carbon\CarbonPeriod;
use Cmixin\EnhancedPeriod;
CarbonPeriod::mixin(EnhancedPeriod::class);
$a = CarbonPeriod::create('2018-01-01', '2018-01-31');
$b = CarbonPeriod::create('2018-02-10', '2018-02-20');
$c = CarbonPeriod::create('2018-02-11', '2018-03-31');
$current = CarbonPeriod::create('2018-01-20', '2018-03-15');
foreach ($current->diff($a, $b, $c) as $period) {
foreach ($period as $day) {
echo $day . "\n";
}
}
This will output all the days that are in $current but not in any of the other periods. (E.g. non-conflicting days)

PHP Tally of hours is different when cross-checked with Google

I am calculating the sum of hours using PHP inside Laravel. When I cross check the result in Google, the results are different.
Here is my the code in the controller:
$times[] = '7:55';
$times[] = '7:55';
$minutes = 0;
foreach ($times as $time) {
list($hour, $minute) = explode(':', $time);
$minutes += $hour * 60;
$minutes += $minute;
}
$hours = floor($minutes / 60);
$minutes -= $hours * 60;
$tally = sprintf('%02d.%02d', $hours, $minutes);
dd($times, $tally);
The output is: "15.50";
but in google, the output is "15.1".
In Excel and Google Sheets, the output is also "15.50".
Which one is correct and how can I modify my code to reflect the same result as google.
15.1 is wrong, 15h 50m is correct. Time summing is not a mystery, you can check it manually: 7h 55m for example is 8h - 5m, so the sum of the 2 is 16h - 10m = 15h 50m.

Update a hashmap value, given a key with getOrDefault

I have a HashMap :
HashMap<string, Integer> hmap = new HashMap<>();
where I want to increase the HashMap value. In order to avoid the nullPointer Exception if the key doesn't exist, I check it! Let's say the data are:
//201803271 - 1000
//201803271 - 1000
//201803272 - 1000
//inside a loop i read the data...
if (hmap.get("201803271") != null) {
hmap.put("201803271", hmap.get("201803271") + 1000);
}else{
hmap.put("201803271", 1000);
}
//end of loop
which works as I get:
201803271 - 2000
201803272 - 1000
But, I read this question How to update a value, given a key in a java hashmap? and there is a solution to use the Java 8 method getOrDefault. I tried it
hmap.put("201803271", count.getOrDefault("201803271", 1000) + 1000)
However, with this solution I get wrong results...
201803271 - 3000
201803272 - 2000
What am I missing?
Java 8 introduced merge method to Map interface just for this type of problem:
hmap.merge("201803271", 1000, Integer::sum);
It means "put 1000 for this key but if this key already has a value add 1000 to it".
The reason your solution wasn't working is that you were getting 1000 by default and then adding 1000 to it. To do this correctly with getOrDefault, you would want to replace 1000 with 0 in getOrDefault. hmap.put("201803271", count.getOrDefault("201803271", 0) + 1000))
You could do it like this:
map.put(key, map.getOrDefault(key, 0) + inc);
or
map.compute(key, (k, v) -> v == null ? inc : v + inc);

user defined sorting functions, how to properly use them?

I have always used this method for accomplishing this kind of sort,
but doesn't look correct to me
usort($array, function($team1, $team2){
$t1 = ($team1->points * 1000) + ($team1->goalsFor - $team1->goalsAgainst);
$t2 = ($team2->points * 1000) + ($team2->goalsFor - $team2->goalsAgainst);
if($t1 == $t2) return 0;
return $t1 > $t2 ? 1 : -1;
});
an alternative to this method is to use str_pad, which does nearly the same
basically what I do is to separate with zeros the sorting subjects
$t1 = 32008; // 32 points with a GD of 8
$t2 = 32003; // 32 points with a GD of 3
but what if a team got a weird goals difference?
$t1 = 32008; // 32 points with a GD of 8
$t2 = 33000; // 32 points with a GD of 1000
clearly this can't happen, but it's an example
is this a good way? how about the 32-64 bit / floating numbers [im]precision limits?
does someone have suggestions for this?
thank you :)
feel free to improve the title
A better approach is this:
function ($team1, $team2) {
if ($team1->points != $team2->points) {
return $team1->points > $team2->points ? 1 : -1;
}
$diff1 = $team1->goalsFor - $team1->goalsAgainst;
$diff2 = $team2->goalsFor - $team2->goalsAgainst;
if ($diff1 != $diff2) {
return $diff1 > $diff2 ? 1 : -1;
}
return 0;
}
Or, in Java (using Guava), I'd write it like this:
public int compare(Team team1, Team team2) {
return ComparisonChain.start()
.compare(team1.getPoints(), team2.getPoints)
.compare(team1.getGoalsFor() - team1.getGoalsAgainst(),
team2.getGoalsFor() - team2.getGoalsAgainst())
.result();
}
Clearly, PHP doesn't have ComparisonChain, but it shouldn't be hard to implement.

Fast editing subtitles file

I like GNU/Linux and writing bash scripts to automatize my tasks. But I am a beginner and have a lot of problems with it. So, I have a subtitle file in format like I this(I'm Polish so it's a Polish subtitles):
00:00:27:W zamierzchłych czasach|ziemia pokryta była lasami.
00:00:31:Od wieków mieszkały|w nich duchy bogów.
00:00:37:Człowiek żył w harmonii ze zwierzętami.
I thing you understanding this simple format. The problem is that in the "movie file", before the movie starts is 1:15 of introduction. I want to add to each subtitle file's line 1:15. So the example should look like this:
00:01:43:W zamierzchłych czasach|ziemia pokryta była lasami.
00:01:46:Od wieków mieszkały|w nich duchy bogów.
00:01:52:Człowiek żył w harmonii ze zwierzętami.
Could you help me to write this script?
BTW I'm Polish and I'm still learning English. So if you cannot understand me, write.
Here's a solution in awk - probably easier than bash for this kind of problem:
#!/usr/bin/awk -f
BEGIN {
FS=":"
}
{
hr = $1
min = $2
sec = $3
sec = sec + 15
if (sec >= 60) {
sec = sec - 60
min = min + 1
}
min = min + 1
if (min >= 60) {
min = min - 60
hr = hr + 1
}
printf "%02d:%02d:%02d:%s\n", hr, min, sec, $4
}
Suggestions for improvement welcome!

Resources