How to get data from database using date? - php

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();

Related

split datetime from array in codeigniter 3

my problem is to get date from array. but there is a time from it.
i get data from model
$data['result'] = $this->MTransaksi->cekpengembalian($id_transaksi);
and i want to get datetime
$date4 = $data['result']->tanggal_kembali;
after get the datetime i should split it with explode in php function
$splitTimeStamp = explode(" ",$date4);
and just get the date from it
$date = date('Y-m-d',strtotime($splitTimeStamp));
but the result in view looks like this
enter image description here
so this is full code i wrote
$data['result'] = $this->MTransaksi->cekpengembalian($id_transaksi);
$date4 = $data['result']->tanggal_kembali;
$splitTimeStamp = explode(" ",$date4);
$date = date('Y-m-d',strtotime($splitTimeStamp));
just want to split date and time
You need to update the code as the explode() return an array.
Below I have assigned the array parameters to separate variables and you can use them individually.
$data['result'] = $this->MTransaksi->cekpengembalian($id_transaksi);
$date4 = $data['result']->tanggal_kembali;
$splitTimeStamp = explode(" ",$date4);
$dateObject = $splitTimeStamp[0];
$timeObject = $splitTimeStamp[1];
$date = date('Y-m-d',strtotime($dateObject));
NOTE: You didn't mention your $date4 how it looks otherwise could have provided a more concrete answer.
Hope it helps

How to pass a date variable into a php query

I've been trying many different ways to pass the dates into the query as a variable, and nothing I've tried has worked. When I put the exact dates in there like below, the query works fine.
$ly = mysqli_query($link,'SELECT SUM(rtype_price) FROM client WHERE date BETWEEN "2017-01-01" AND "2017-12-31"');
while($row1 = mysqli_fetch_array($ly))
{
$ly_cash = $row1['SUM(rtype_price)'];
}
For example, I've been trying something like this:
$ly_start = date('Y-m-d', strtotime('first day of previous year'));
$ly_end = date('Y-m-d', strtotime('last day of previous year'));
then passing $ly_start and $ly_end into the query like this:
$ly = mysqli_query($link,'SELECT SUM(rtype_price) FROM client WHERE date BETWEEN "$ly_start" AND "$ly_end"');
Inverting the quotes did the trick. This works:
$ly = mysqli_query($link,"SELECT SUM(rtype_price) FROM client WHERE paid = 1 AND date BETWEEN '$ly_start' AND '$ly_end'");
while($row1 = mysqli_fetch_array($ly))
{
$ly_cash = $row1['SUM(rtype_price)'];
}
Thank you ild_flue

Laravel 4.2 WhereBetween Not Working

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.

PHP displaying date is one day forward after format

When I retrieve a date from a my_sqli query and format it it becomes one day forward. The date is saving correctly to the server, and echoing it before the new format is correct.
$date = "SELECT date FROM blogtable WHERE id = $artID";
$dateEx = mysqli_query($con, $date);
while($dateGet = mysqli_fetch_array($dateEx))
{
//This is in YYYY-mm-dd
$dateGet['date'];//If I echo this, it is correct
}
$source = $dateGet;
$newDate = new DateTime($source);
echo $newDate->format('M-d-Y');
So for example if I tried to use it today(the 24th), it would save correctly, but after the format, display as the 25th.
Wrikken's suggestion worked, changing the statement in the while to $source=$dateGet['date']; and deleting the $source = $dateGet;.

MYSQL Date range query not working correctly

I have a problem getting certain data from my database by querying a date range. In my database i have a DATE type field within the format YYYY-MM-DD. I want to get all data within a date range of today + 2 weeks (Expiring).
I have wrote:
$format = 'Y-m-j';
$date = date ( $format );
$new = date ( $format, strtotime ( '+14 day' . $date ) );
$start = date("Y-m-d", strtotime($new));
$today = date('Y-m-d');
$q = "SELECT * FROM listing WHERE dd_end BETWEEN '".$today."' AND '".$start."'";
while($row = mysql_fetch_assoc($q)){
$listing_id = $row['listing_id'];
echo "$listing_id";
}
So what I want to achieve is for the query to pull all the rows from now until 5th October. I've echo'd the variables and they show they're in the correct form (YYYY-MM-DD) to compare within the database but no results are returning.
Any help would be greatly appreciated. Many thanks in return.
Well, assuming that the mysql database has the same date that your server, you could let the mysql database do all the date calculations.
A little something like this:
SELECT *
FROM listing
WHERE dd_end BETWEEN CURDATE() AND (CURDATE() + INTERVAL 14 DAY)
On the other hand, i think Paul S's answer may fix your problem.
Edit:
You forgot to call mysql_query before the mysql_fetch_assoc() function.
$result = mysql_query($q);
while ($row = mysql_fetch_assoc($result))
{
$listing_id = $row['listing_id'];
echo "$listing_id";
}
If dd_end is a date you may want to read a certain section on the MySQL docs: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between
For best results when using BETWEEN with date or time values, use
CAST() to explicitly convert the values to the desired data type.
Examples: If you compare a DATETIME to two DATE values, convert the
DATE values to DATETIME values. If you use a string constant such as
'2001-1-1' in a comparison to a DATE, cast the string to a DATE.
May this is the right way ?
$start = date("Y-m-d", strtotime('+14 day' . $date));
Read:
http://php.net/manual/en/function.strtotime.php
strtotime has a second argument.
$format = 'Y-m-j';
$date = date ( $format );
$new = date ( $format, strtotime ( '+14 day' . $date ) );
$start = date("Y-m-d", strtotime($new));
Should be:
$new = strtotime('+14 day', time());
$start = date("Y-m-d", $new);
$today = date('Y-m-d');
$q = mysql_query("SELECT * FROM listing WHERE dd_end BETWEEN '".$today."' AND '".$start."'");
while($row = mysql_fetch_assoc($q)){
$listing_id = $row['listing_id'];
echo "$listing_id";
}

Categories