How to check if number is a decimal and run function laravel - laravel

I use Ajax to search my data and when it runs it removes price formatting from 49.99 to 4999. I can solve the problem by using ${{number_format($product->price / 100, 2)}} in the view file but the problem is if the price is displaying correctly as 49.99 and the function runs then it now displays as 0.50. How can I run a check so that this function only attempts to format price if it doesn't have decimal, only if its a int like 4999
I tried
if(is_float($product->price)){
echo $product->price;
} else {
echo number_format($product->price / 100, 2);
}

This worked for me
if (is_numeric($product->price) && floor($product->price) != $product->price) {
echo $product->price;
} else {
echo number_format($product->price / 100, 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)

Compare time using Laravel

In my Laravel application I want to compare times
If time was greater than 23:30 and less than 6:00 returns true
else return false
$time1 = strtotime('23:30:00');
$time2 = strtotime('06:00:00');
$time3 = strtotime('01:30:00');
if ($time3>=$time1 && $time2>$time1 ) {
var_dump('yes');
}
else {
var_dump('no');
}
I would suggest using Carbon to compare date and datetime values:
$time1 = Carbon::createFromTimeString('23:30:00');
$time2 = Carbon::createFromTimeString('06:00:00');
$time3 = Carbon::createFromTimeString('01:30:00');
if ($time3->gte($time1) && $time2->gt($time1)) {
var_dump('yes');
} else {
var_dump('no');
}
Use Carbon, it has functionality to create from a certain format, with the createFromFormat() method.
$time1 = Carbon::createFromFormat('H:i:s', '23:30:00');
$time2 = Carbon::createFromFormat('H:i:s', '06:00:00');
$time3 = Carbon::createFromFormat('H:i:s', '01:30:00');
Carbon has comparisons in built, with gt() being greater than and lt() being less than. Adding an e to the call as lte() would be less than equals. Or you can be more explicit, look through this section in the documentation.
if ($time3->gte($time1) && $time2->gt($time1)) {
//
}
else {
//
}

Amibroker AFL code Buy Sell status is not fetching with if condition

I have AFL which is working fine for crude oil. out of 10 trades, 8 trades are targets hitting. I have code for place orders auto trades. the auto trade code is working fine with other AFL codes but the problem is in below algorithm the BUY and SELL Boolean value is not giving to IF condition. But IIF(Buy .... conditions are working fine.
My main question is why BUY Sell True or false is not working in the last status in AFL. Kindly help me to resolve this.
_SECTION_BEGIN("T+4 day ");
Title = " ..:: duy ::.. - Filter of Stock " + " " + FullName() + " " + Date( ) ;
// 4-Day-Range Switch
prev=AMA2(C,1,0);
d=IIf(C>Ref(Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),-1),Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),
IIf(C<Ref(Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),-1),Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),PREV));
a=Cross(Close,d);
b=Cross(d,Close);
state=IIf(BarsSince(a)<BarsSince(b),1,0);
s=state>Ref(state,-1);
ss=state<Ref(state,-1);
sss=state==Ref(state,-1);
col=IIf(state == 1 ,51,IIf(state ==0,4,1));
Plot(C,"",Col,128);
Buy=s;
Sell=ss;
PlotShapes( shapeUpArrow * s ,6,0,L);
PlotShapes( shapeDownArrow *ss ,4,0,H);
dist = 0.8*ATR(10);
dist1 = 2*ATR(10);
for( i = 0; i < BarCount; i++ )
{
if( Buy )
{
PlotText( "\nBuy:" + L[ i ] + "\nT= " + (L*1.005) + "\nSL= " + (L*0.9975), i, L[ i ]-dist, colorGreen, colorWhite );
}
if( Sell )
{
PlotText( "Sell:" + H[ i ] + "\nT= " + (H*0.995) + "\nSL= " + (H*1.0025), i, H[ i ]+dist1, colorRed, colorWhite );
}
}
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
if ( LastValue(Buy)==1)
{
quantity=2;
orderId=placeOrderFuture("MCX", "FUTCOM", ChartSymbol, "BUY", "INTRADAY", "MARKET", quantity, 0, defaultTriggerPrice(), "19-APR-2018", defaultStrategyId(), defaultComments());
//orderId = placeOrderUsingParams(tradeType, AT_ORDER_TYPE, AT_QUANTITY, buyPrice, defaultTriggerPrice(), 1);
}
if ( LastValue(Sell) == 1 )
{
quantity=2;
orderId=placeOrderFuture("MCX", "FUTCOM", ChartSymbol, "SELL", "INTRADAY", "MARKET", quantity, 0, defaultTriggerPrice(), "19-APR-2018", defaultStrategyId(), defaultComments());
//orderId = placeOrderUsingParams("SELL", AT_ORDER_TYPE, AT_QUANTITY, sellPrice, defaultTriggerPrice(), 1);
}
LastValue documentation
With if statements, you need to specify a specific bar. And according to the documentation, LastValue may look into the future. I can't say for sure what's happening with your code, but the loops/if/switch can be tricky. This tutorial Looping in Amibroker might give you some insights into how they work.
You may try SelectedValue instead. If you haven't got any bars selected, it automatically defaults to the last bar. I use this for my realtime trading.
bi = SelectedValue(BarIndex());
if(Buy[bi])
{
...
}
On an unrelated note, your text plots aren't going to plot unfiltered signals, put your ExRem code under your initial Buy and Sell conditions.

Cronjob in Laravel 5

I have the job table contains post_at,day_open,block properties
The post_at property is the day when user post the job (Ex:2015-11-24)
The day_open property is the day that job is open (Ex:10)
The block property can contain 2 values 0 or 1. If post_at + day_open > right now the block value will change from 0 to 1
So how can I do that using cronjob?
First check wheather the post_at,day_open are edit in current date
$date = date('Y-m-d');
$job= DB::table('job') ->select('post_at','day_open','block')->where('updated_at',$date) -first();
$id=$job->id;
if(isset($job) && !empty($job))
{
$finding=CronJobCotroller::pushNotify($id);
}
if it's true it go to function pushNotify under the CronJobCotroller,here we write the coding for changing block field value 0 to 1.
public function pushNotify($id) {
$job= DB::table('job') ->select('block')->where('id',$id) ->get();
if(isset($job) && !empty($job))
{
foreach ($job as $cb)
{
$job= DB::table('job')->update('block',1)->where('block',0);
}
}
}

PHP Function to Calculate Relative Time (Human Readable / Facebook Style)

function RelativeTime($timestamp) {
$difference = time() - $timestamp;
$periods = array(
"sec", "min", "hour", "day", "week", "month", "years", "decade"
);
$lengths = array("60", "60", "24", "7", "4.35", "12", "10");
if ($difference > 0) { // this was in the past
$ending = "ago";
} else { // this was in the future
$difference = -$difference;
$ending = "to go";
}
for ($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if ($difference != 1) $periods[$j] .= "s";
$text = "$difference $periods[$j] $ending";
return $text;
}
I found the above PHP function on the interwebs. It seems to be working pretty well, except it has issues with dates far in the future.
For example, I get looping PHP error
division by zero
for $difference /= $lengths[$j]; when the date is in 2033.
Any ideas how to fix that? The array already accounts for decades, so I am hoping 2033 would result in something like "2 decades to go".
The problem is that the second array $lengths contains 7 elements so when executing the last iteration of the loop (after deviding by 10 - for the decades) $j = 7, $lengths[7] is undefined, so converted to 0 and therefore the test $difference >= $lengths[$j] returns true. Then the code enters an infinite loop. To overcome this problem, just add one more element to the $lengths array, say "100", so the for loop to terminate after processing the decades. Note that dates can be represented in UNIX timestamp if they are before January 19, 2038. Therefore you cannot calculate dates in more than 4 decates so 100 is sufficiant to break from the loop.

Resources