Laravel 4.2 WhereBetween Not Working - php

I have a table like below structure,
The entry_added_date is storing date equivalent field, (2015-07-27).
Now I want to filer that entries in between two dates with this field.
I tried the below code,
$start = Input::get('start');
$end = Input::get('end');
$arr = DB::table('otc_revenue_entries')
->whereBetween('entry_added_date',array($start,$end))
->get();
return Response::json(['data',$arr]);
Where is the wrong in my code, ?

Check Below code..
$startdate = new DateTime($start);
$start_date = $startdate->format("Y-m-d");
$enddate = new DateTime($end);
$end_date = $enddate->format("Y-m-d");
$arr = DB::table('otc_revenue_entries')
->leftJoin('otc_users','otc_users.user_id','=','otc_revenue_entries.entry_added_by')
->leftJoin('otc_branches','otc_branches.branch_id','=','otc_revenue_entries.entry_branch')
->whereBetween('entry_added_date',array($start_date,$end_date))
->get();
return Response::json(['data',$arr]);

Make sure your $start and $end are actually the same format as your entry_added_date, the comparison might be failing due to that.

Related

get last month and year using carbon laravel

This might be a simple, I've $month and $year, but I'm trying to get lastMonthYear and lastToLastMonthYear.
$date = Carbon::create($year, $month)->startOfMonth();
$sameMonthLastYear = $date->subYear()->format('Ym');
$lastMonthYear = $date->subMonth()->format('Ym');
$LastToLastMonthYear = $date->subMonth()->format('Ym');
Considering $month=9 and $year=2021, I'm trying to get
sameMonthLastYear = 202009
lastMonthYear = 202108
lastToLastMonthYear = 202107
but I'm getting
sameMonthLastYear = 202009
lastMonthYear = 202008
lastToLastMonthYear = 202008
Because $date value is keep updating for every line. Is there any way to get the expected result?
The right way is to use CarbonImmutable:
$date = CarbonImmutable::create($year, $month);
$sameMonthLastYear = $date->subYear()->format('Ym'); //202009
$lastMonthYear = $date->subMonth()->format('Ym'); //202108
$LastToLastMonthYear = $date->subMonths(2)->format('Ym'); //202107
With CarbonImmutable, the changes you make to $date are not persisted.
I also removed the startOfMonth method, it was useless since by default, the Carbon instance will be:
Carbon\CarbonImmutable #1630454400 {#5430
date: 2021-09-01 00:00:00.0 UTC (+00:00),
}
You can use copy() method:
$date = Carbon::create($year, $month)->startOfMonth();
$sameMonthLastYear = $date->copy()->subYear()->format('Ym');
$lastMonthYear = $date->copy()->subMonth()->format('Ym');
$LastToLastMonthYear = $date->copy()->subMonths(2)->format('Ym');

How to get data from database using date?

I want to selecting and showing data based on current date. I have made query selected for this, but it not working
this is my date data on table :
this is my query:
$now = date('Y-m-d');
$trans = FA_transaction::where('assign_date', $now)->get();
when i execute the query, i don't get any data
You can do :
$trans = FA_transaction::whereDate('date', Carbon::today())->get();
OR if you want to use MySQL's CURDATE function, you can do :
$trans = FA_transaction::->whereRaw('Date(date) = CURDATE()')->get();
You can use whereDate function to filter out records based on date try this!
$now = date('Y-m-d');
$trans = FA_transaction:: whereDate('date','=',$now)->get();
You can use it
$now = Carbon::today();
$trans = FA_transaction::where('assign_date', $now)->get();
OR
$now = Carbon::now()->format('Y-m-d');
$trans = FA_transaction::where('assign_date', $now)->get();

Initialize DateTime 01/01/Current year and 31/12/Current year

I'm struggling with the PHP-DateTime Object.
I just want to set the beginn date to the start of the current year and the end date to the end of the current year.
Is there an easy way to create such a date in one line?
$beg = new DateTime();//TO-DO 01.01.ThisYear
$this->beginDate = $beg;
$end = new DateTime();//TO-DO EndOfThisYear
$this->endDate = $end;
I'm using an older PHP-Version, don't know exactly which one...something above 5.
Sure:
$beg = new DateTime(date('Y-01-01'));
$end = new DateTime(date('Y-12-31'));

Is it possible to change data type from String to Date

I'm getting in trouble where I was coding for connection using OpenX API with XML-RPC2. I get the problem that the data type is required by the fire function is the dateTime.iso8601.
This is my code:
$sdatetime = new DateTime('2013-01-01 00:00:00');
$edatetime = new DateTime('2013-06-01 00:00:00');
$startDate = $sdatetime->format(DateTime::ISO8601);
$endDate = $edatetime->format(DateTime::ISO8601);
try {
$result = $aClient->agencyPublisherStatistics($sessionId, 1, $startDate, $endDate);
print_r($result);
} catch (XML_RPC2_FaultException $e) {
die('Exception #' . $e->getFaultCode() . ' : ' . $e->getFaultString());
}
This is result error, when I run script above:
Exception #3 : Incorrect
parameters passed to method: Wanted dateTime.iso8601, got string at
param 3
If I run print_r(gettype($startDate)); I get the type data is string not date.
My question, for variables $startDate and $endDate how to make their data type to be dateTime.iso8601 or date rather than string.
Thanks.
it looks like your agencyPublisherStatistics requires specific XML_RPC2_Value date object. You cancreate this by using.
$startDate = XML_RPC2_Value::createFromNative($startDate, ‘datetime’);
same for the end date.. let me know if this works..
Try this,
$sdatetime = date(DATE_ISO8601, strtotime('2013-01-01 00:00:00'));
$edatetime = date(DATE_ISO8601, strtotime('2013-06-01 00:00:00'));
OR
Check below links,
http://pear.php.net/manual/en/package.webservices.xml-rpc2.client.php
https://bugs.php.net/bug.php?id=51950
may this help you.
use DateTime::setISODate
$sdatetime = new DateTime('2013-01-01 00:00:00');
$edatetime = new DateTime('2013-06-01 00:00:00');
$startDate = $sdatetime->setISODate(2013);
$endDate = $edatetime->setISODate(2013);

Returning months difference as integer

I am trying to return month difference as integer and call if to table column. My code:
function ETA($ArrivalDate, $pattern = 'mysql'){
$patterns = array(
'eu' => 'd/m/Y',
'mysql' => 'Y-m-d',
'us' => 'm/d/Y',
);
$CurrentDate = date("Y-m-d");
$ArrivalDate = $variants_data['ArrivalDate'];
$diff = $ArrivalDate->diff($CurrentDate);
return $diff->y;
}
The I call it with
<td>'.$_GET['ETA'].'</td>
But there is nothing returned, what am I doing wrong here?
You can get what you want in a simpler approach using the DateTime object in PHP:
function ETA($ArrivalDate){
$currentDate = new DateTime();
$arrivalDate = new DateTime($ArrivalDate);
$interval = $currentDate->diff($arrivalDate);
return $interval->format('%m');
}
See working example: http://3v4l.org/UlPQo
If you don't pass in an appropriate format $ArrivalDate you will get an exception thrown, so you need to wrap the call in a Try/Catch.
See DateTime Interval Format for more on the return value.
Try this simple one
<?php
//for months
$monthdiff=floor((abs(strtotime(date("d/m/Y")) - strtotime($ArrivalDate))/(60*60*24*30)));
//for days
$daydiff=floor((abs(strtotime(date("d/m/Y")) - strtotime($ArrivalDate))/(60*60*24)));
?>

Categories