addBetweenCondition today not returning - php

I tried a couple of ways. It's not returning the correct number. It either gives me the default number which is set at the beginning or returns the searched criteria but ignores the store_id.
$percent = 8;
$today = date('Y-m-d');
$criteria = new CDbCriteria();
$criteria->compare('store_id', 253);
// $criteria->condition = 'store_id=:store_id';
// $criteria->params = array('store_id'=>253);
// the bottom addcondition ignores that store id, and gives me what it finds btwn dates
// $criteria->addCondition = 'CURRENT_DATE BETWEEN date_from AND date_to';
// addbetweencondition doesn't find anything between dates
$criteria->addBetweenCondition($today, 'date_from', 'date_to');
$override = Model::model()->find($criteria);
if ($override) {
$override_percent= $override->percent/100;
}
print_r ($override_percent);

You are using addBetweenCondition wrong. The first three parameters should be:
the column to match against
the start date
the end date
I think it should be something like the code below (untested):
$date_from = date('Y-m-d');
$date_to = date('Y-m-d 23:59:59');
$criteria->addBetweenCondition('column_name_that_contains_date', $date_from, $date_to);
Read more about the function:
http://www.yiiframework.com/doc/api/1.1/CDbCriteria#addBetweenCondition-detail

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

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";
}

strtotime php mysql

All I'm trying to do is make the php file accumulate the end date from the sub date. I don't understand why this strtotime function isn't working. My database stores dates as "Y-m-d".
here's the code:
//disguised for security reasons
$db = mysql_connect("*******", "*******","********");
mysql_select_db("*******",$db);
$getad = mysql_query("SELECT id, annual_sub_date FROM members WHERE annual_sub_date!=null", $db);
while ($gad = mysql_fetch_array($getad)) {
$id = $gad['id'];
$asd = $gad['annual_sub_date'];
$aedate_time = strtotime('+1 year', $asd);
$aedate = date("Y-m-d", $aedate_time);
mysql_query("UPDATE members SET annual_end_date='$aedate', annual_active='Y' WHERE id='$id'");
}
---------SOLVED IT---------
I went and played XBox Split/Second for a bit and then realised the issue. My mind went back to PHP/MySQL 101. I coded everything right except the "!=null" part.
//Wrong Way
$getad = mysql_query("SELECT id, annual_sub_date FROM members WHERE annual_sub_date!=null", $db);
//Correct Way
$getad = mysql_query("SELECT id, annual_sub_date FROM members WHERE annual_sub_date IS NOT NULL", $db);
Now everything works :) That's the issues you can expect coding at 5:01am.
The first argument to strtotime is an absolute or relative date as a string, the second argument is an integer timestamp. You're giving it a relative date (string) as the first argument and an absolute date (also string) as the second. You need to convert $asd to a timestamp using strtotime first.
$aedate_time = strtotime('+1 year', strtotime($asd));
BTW, you could do the whole date calculation and updating in SQL with a single query, no need to take the long way around through PHP.
It's because strtotime requires timestamp as second argument and not string date in Y-m-d.
Just try code snippet below to see what I ment.
$gad = array('annual_sub_date' => '2010-11-21');
// wrong
// $asd = $gad['annual_sub_date'];
// good; convert to timestamp
list($year, $month, $day) = explode('-', $gad['annual_sub_date']);
$asd = mktime(0, 0, 0, $month, $day, $year);
$aedate_time = strtotime('+1 year', $asd);
$aedate = date("Y-m-d", $aedate_time);
echo $aedate . "\n";

Categories